Android開発20――複数のボタンクリックイベントを単一のリスナーで傍受


一、単一ボタンクリックイベントの傍受
方法1
/**
  • *ネットワークから画像を取得
  • *  
  • *@author徐越
  • *  
  • */

  • publicclass MainActivity extends Activity  
  • {  

  • private EditText txtPath;  
  • private Button btnShowImage;  

  • private ImageView imgView;  

  • @Override
  • publicvoid onCreate(Bundle savedInstanceState)  

  •    {  
  • super.onCreate(savedInstanceState);  

  •        setContentView(R.layout.main);  
  •        txtPath = (EditText) this.findViewById(R.id.txtPath);  

  •        btnShowImage = (Button) this.findViewById(R.id.btnShowImage);  
  •        imgView = (ImageView) this.findViewById(R.id.imgView);  

  •        btnShowImage.setOnClickListener(new ShowImageListener());  
  •    }  

  • privatefinalclass ShowImageListener implements View.OnClickListener  

  •    {  
  • @Override

  • publicvoid onClick(View v)  
  •        {  

  • //ピクチャパス
  •            String path = txtPath.getText().toString();  

  • try
  •            {  

  • //画像のバイナリデータ取得
  • byte[] imgdata = ImageService.getImage(path);  

  • //BitmapファクトリによるBitmap の生成
  •                Bitmap bitmap = BitmapFactory.decodeByteArray(imgdata, 0, imgdata.length);  

  • //imageView Bitmapを受信し、を表示
  •                imgView.setImageBitmap(bitmap);  

  •            }  
  • catch (Exception e)  

  •            {  
  •                Toast.makeText(MainActivity.this、「画像の読み取りに失敗しました」,Toast.LENGTH_SHORT).show();  

  •            }  
  •        }  

  •    }  
  • }

  • 方法2
    レイアウトページでこのボタンにandroid:onClick=「showImage」を付け、その要素のActivityにshowImage(View v)を入れる方法を表示して操作します.
    二、複数ボタンクリックイベントの傍受
    方法1
    Activityでは、最初の大きなタイトルのメソッドに従って、ボタンごとにリスニングクラスまたはリスニングメソッドを書きます.
    方法2
    すべてのボタンのクリックイベントを1つのリスナーで傍受
    /**
  • *照会番号帰属地
  • *  
  • *@author徐越
  • *  
  • */

  • publicclass MainActivity extends Activity implements View.OnClickListener  
  • {  

  • private EditText txtPhone;  
  • private TextView lblAddress;  

  • private Button btnQuery;  
  • private Button btnReset;  

  • private CallAddressQueryService callAddressQueryService = new CallAddressQueryService();  
  • privatefinalint CLICK_QUERY = 1;  

  • privatefinalint CLICK_RESET = 2;  

  • @Override
  • publicvoid onCreate(Bundle savedInstanceState)  

  •    {  
  • super.onCreate(savedInstanceState);  

  •        setContentView(R.layout.main);  
  •        lblAddress = (TextView) this.findViewById(R.id.lblAddress);  

  •        txtPhone = (EditText) this.findViewById(R.id.txtPhone);  
  •        btnQuery = (Button) this.findViewById(R.id.btnQuery);  

  •        btnReset = (Button) this.findViewById(R.id.btnReset);  
  •        btnQuery.setOnClickListener(this);  

  •        btnQuery.setTag(CLICK_QUERY);  
  •        btnReset.setOnClickListener(this);  

  •        btnReset.setTag(CLICK_RESET);  
  •    }  

  • @Override

  • publicvoid onClick(View v)  
  •    {  

  • int tag = (Integer) v.getTag();  
  • switch (tag)  

  •        {  
  • case CLICK_QUERY:  

  •                query();  
  • break;  

  • case CLICK_RESET:  
  •                reset();  

  • break;  
  •        }  

  •    }  

  • publicvoid query()  
  •    {  

  •        String phone = txtPhone.getText().toString();  
  • try

  •        {  
  •            lblAddress.setText(「クエリー中」); 

  •            String address = callAddressQueryService.getCallAddress(phone);  
  •            lblAddress.setText(address);  

  •        }  
  • catch (Exception e)  

  •        {  
  •            e.printStackTrace();  

  •            Toast.makeText(this,“クエリに失敗しました”,Toast.LENGTH_LONG).show();  
  •        }  

  •    }  

  • publicvoid reset()  
  •    {  

  •        txtPhone.setText("");  
  •        lblAddress.setText("");  

  •    }  
  • }

  • 本文は「IT徐デブのコラム」ブログから出ていますので、ぜひこの出典を残してくださいhttp://woshixy.blog.51cto.com/5637578/1093936