How to tell which button was clicked in onClick()

南楼画角 提交于 2019-12-17 19:29:51

问题


I have multiple buttons in an Android app. I want to know, in the Java code, which button was clicked. As far as I can tell, this is accomplished with a single method like this:

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

And inside that method, you have to figure out which button was clicked. Is that correct?

If so, how do I tell which was clicked? I do have the various Button objects returned by findViewById(). I just don't know how to use them to tell which button was clicked.


回答1:


Implement View's OnClickListner in your activity class. Override on 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 have 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 switch case you can check which button is clicked.

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's the complete 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;  


    }

}
 }



回答2:


If so, how do I tell which was clicked?

Old friend switch will help you to 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 ID so you can simply handle which Button is clicked via its ID in switch written above.

view.getId() returns ID of widget.




回答3:


There are different 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 at android:id in your xml. You can use a switch statement to compare the values to decide what to do and switch on the id. You can also assing an onClick() in your xml with each button.

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

then in your java code you can have

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

And the view clicked here will be the button you assigned this onClick to in your xml




回答4:


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!
    }
});



回答5:


You can use ButterKnife library ( http://jakewharton.github.io/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;
}



回答6:


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;      
    }
}
}


来源:https://stackoverflow.com/questions/15732307/how-to-tell-which-button-was-clicked-in-onclick

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!