How to determine which button was clicked in onClick ()

I have several buttons in an android app. I want to know in Java code which button was clicked. As far as I can tell, this is done using one method:

public void onClick(View view) { // Do something } 

And inside this method you have to figure out which button was pressed. It is right?

If so, how can I say what was pressed? I have various Button objects returned by findViewById (). I just don't know how to use them to indicate which button was clicked.

+4
source share
6 answers

Deploy ViewClickListner in your activity class. Override the click method.

  Button b1= (Button) findViewById(R.id.button1); //find your button id defined in your xml. b1.setOnClickListener(this); // You have button OnClickListener implemented in your activity class. //this refers to your activity context. 

I used a toast message.

http://developer.android.com/guide/topics/ui/notifiers/toasts.html

  Toast.makeText(MainActivity.this,"button1", 1000).show(); //display a toast using activity context ,text and duration 

Using the switch case, you can check which button is pressed.

In your onClick method.

 switch(v.getId()) //get the id of the view clicked. (in this case button) { case R.id.button1 : // if its button1 //do something break; } 

Here is the full code.

 public class MainActivity extends Activity implements OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button b1= (Button) findViewById(R.id.button1); Button b2= (Button) findViewById(R.id.button2); Button b3= (Button) findViewById(R.id.button3); b1.setOnClickListener(this); b2.setOnClickListener(this); b3.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch(v.getId()) { case R.id.button1 : Toast.makeText(MainActivity.this,"button1", 1000).show(); break; case R.id.button2 : Toast.makeText(MainActivity.this,"button2", 1000).show(); break; case R.id.button3 : Toast.makeText(MainActivity.this,"button3", 1000).show(); break; } } } 
+12
source

If so, how can I say what was clicked on?

An old familiar switch will help you achieve your goal:

 public void onClick(View view) { switch (view.getId()) { case R.id.btn1: // do your stuff for btn1 break; case R.id.btn2: // do your stuff for btn2 break; ... } } 

Explanation: Each widget has an ID , so you can simply edit which button to click on its ID in the switch above.

view.getId() returns the widget ID .

+4
source

There are several ways to handle this. With what you currently have, you can use

  public void onClick(View view) { // Do something view.getId(); } 

which will return the value in android:id in xml . You can use the switch to compare values ​​to decide what to do and enable id . You can also define onClick() in xml with each button .

 <Button ... android:onClick="functionName"/> 

then in your java code you can

 public void functionName(View view) { // Do something } 

And clicking view will be the button that you assigned to this onClick in xml

+4
source

You can also use anonymous inner classes for each button:

 Button b1= (Button) findViewById(R.id.button1); Button b2= (Button) findViewById(R.id.button2); b1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // button 1 was clicked! } }); b2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // button 2 was clicked! } }); 
+3
source

You can use the ButterKnife library ( http://jakewharton.imtqy.com/butterknife/ ):

 //Fragment @InjectView(R.id.dialog_userinput) public EditText userinput; @InjectView(R.id.dialog_passinput) public EditText passinput; @OnClick(R.id.dialog_login) public void login(View view) { //do stuff this.dismiss(); } @OnClick(R.id.dialog_cancel) public void cancel(View view) { //do stuff this.dismiss(); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.dialog_layout, container); ButterKnife.inject(this, view); return view; } 
+1
source
 implements public class ProgramsFiles extends AppCompatActivity implements View.OnClickListener { public void onClick(View view) { switch (view.getId()) { case R.id.btnA: //ExampleA break; case R.id.btnB: //ExampleB break; } } } 
+1
source

All Articles