I. Monitoring of Single Button Click Events
Method 1
/** * Getting pictures from the network** @author xy * */public class MainActivity extends Activity{ private EditText txtPath; private Button btnShowImage; private ImageView imgView; @Override public void 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()); } private final class ShowImageListener implements View.OnClickListener { @Override public void onClick(View v) { // Picture path String path = txtPath.getText().toString(); try { // Get the binary data of the picture byte[] imgdata = ImageService.getImage(path); // Using Bitmap Factory to Generate Bitmap Bitmap bitmap = BitmapFactory.decodeByteArray(imgdata, 0, imgdata.length); // Image View receives Bitmap and displays it imgView.setImageBitmap(bitmap); } catch (Exception e) { Toast.makeText(MainActivity.this, "Failed to read the picture", Toast.LENGTH_SHORT).show(); } } }}
Method two
Add android:onClick="showImage" to the button in the layout page, and then display the method of adding showImage(View v) to the Activity of the element, which operates in the method.
2. Monitoring of Multiple Button Click Events
Method 1
In Activity, write a listening class or method for each button according to the method of the first heading.
Method two
Use a listener to listen for all button clicks
/** * Inquiry Number Attribution** @author Xu Yue **/public class MainActivity extends Activity implements View.OnClickListener{ private EditText txtPhone; private TextView lblAddress; private Button btnQuery; private Button btnReset; private CallAddressQueryService callAddressQueryService = new CallAddressQueryService(); private final int CLICK_QUERY = 1; private final int CLICK_RESET = 2; @Override public void 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 public void onClick(View v) { int tag = (Integer) v.getTag(); switch (tag) { case CLICK_QUERY: query(); break; case CLICK_RESET: reset(); break; } } public void query() { String phone = txtPhone.getText().toString(); try { lblAddress.setText("Query"); String address = callAddressQueryService.getCallAddress(phone); lblAddress.setText(address); } catch (Exception e) { e.printStackTrace(); Toast.makeText(this, "Query failed", Toast.LENGTH_LONG).show(); } } public void reset() { txtPhone.setText(""); lblAddress.setText(""); }}