This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Mar 10th, 2014  |  syntax: None  |  size: 1.23 KB  |  views: 54  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. public void onClick(View v) {
  2.   switch(v.getId()){
  3.     case R.id.Button1:
  4.       break;
  5.     case R.id.Button2:
  6.       break;
  7.     case R.id.Button3:
  8.       break;
  9. //....
  10.   }
  11. }
  12.        
  13. @Override
  14. public void onClick(View view) {
  15.     int Id = view.getId();
  16. }
  17.        
  18. Button button1 = (Button)findViewById(R.id.Button01);
  19.     Button button2 = (Button)findViewById(R.id.Button02);
  20.  
  21.     private class myListener implements OnClickListener
  22.     {
  23.  
  24.         @Override
  25.         public void onClick(View arg0) {
  26.  
  27.             if (arg0 == button1)
  28.             {
  29.                 //button 1 clicked
  30.             }
  31.             else if (arg0 == button2)
  32.             {
  33.                 //button 2 clicked
  34.             }
  35.             //etc
  36.         }
  37.  
  38.     }
  39.  
  40.     myListener listener = new myListener();
  41.  
  42.     button1.setOnClickListener(listener);
  43.     button2.setOnClickListener(listener);
  44.        
  45. <Button android:id="@+id/my_button1"
  46.         android:layout_width="wrap_content"
  47.         android:layout_height="wrap_content"
  48.         android:text="@string/my_button_text"/>
  49.        
  50. final Button button1 = (Button) findViewById(R.id.my_button1);
  51.      button.setOnClickListener(new View.OnClickListener() {
  52.           public void onClick(View v) {
  53.                // Perform action on click
  54.           }
  55.      });
clone this paste RAW Paste Data