Button on click listener

2019-08-09 15:10发布

问题:

I come from the iOS world and just started with Android. I followed a java training so I know how to create a method, but I don't get the action one.

What I would like to do is to create a method action from a button and I can't find anywhere how to do that (obviously looking in the wrong place). When I click on button 1, I want to create a method with inside only a=1 and when I click on button 2, I want a=2 (then I can decide what to do when a is 1 or 2).

I created my buttons in Acitivity_main.xml and gave a method name in OnClick, but that's as far as I went.

It seems to be so basic to do when I compare with iOS that I don't understand why I can't find how to do it.

Would a nice soul point me in the right direction for creating an action please?

回答1:

You have three options:

common in 1,2) You need to assign an id to each of your buttons in the layout XML file

<Button android:id="@+id/my_button1"
..........
/>
<Button android:id="@+id/my_button2"
..........
/>

1) In the activity's onCreate() method after setContentView() you need to set a new OnClickListener for each button.

 public class MyActivity extends Activity  {
    int a;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button myButton1 = (Button) findViewById(R.id.my_button1);
        Button myButton2 = (Button) findViewById(R.id.my_button2);

        myButton1.setOnClickListener( new OnClickListener() {
          @Override
            public void onClick(View v) {
                // Do what you want here
                a = 1;
            }
        });

        myButton2.setOnClickListener( new OnClickListener() {
           @Override
            public void onClick(View v) {
                // Do what you want here
                a = 2;
            }
        });
      }

2) As you see in the first approach, we need to make a new Object from the OnClickListener for each button. We can mix all that into one OnClickListener for performance and readability reasons.

public class MyActivity extends Activity implements View.OnClickListener {
    int a;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button myButton1 = (Button) findViewById(R.id.my_button1);
        myButton1.setOnClickListener(this);
        Button myButton2 = (Button) findViewById(R.id.my_button2);
        myButton2.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.my_button_1:
                a = 1;
                break;
            case R.id.my_button_2:
                a = 2;
                break;
        }
    }
    ...
}

3) You don't need to assign an id for this option you just need to assign the method name in the XML and then implement the same method in the activity with the exact same name but it must take a View object as an argument.

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

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

and then in your activity just write the methods.

public class MyActivity extends Activity implements View.OnClickListener {
    int a;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void button1Click(View v) {
       a = 1;
    }


    public void button2Click(View v) {
       a = 2;
    }
}

That's all your options. I personally prefer number 2.



回答2:

You've two options to handle the buttons, from the documentation:

a) Declare and define you method in you java file

    int a = 0;
    final Button button = (Button) findViewById(R.id.button_id);
             button.setOnClickListener(new View.OnClickListener() {
                 public void onClick(View v) {
                     a = 1;
                 }
             });

b) or define you method in you xml layout:

 <Button
     android:layout_height="wrap_content"
     android:layout_width="wrap_content"
     android:text="@string/self_destruct"
     android:onClick="selfDestruct" />

And then in you java file:

 int a = 0;
 public void selfDestruct(View view) {
     a = 1;
 }


回答3:

First assign the button in yours xml file like below:-

<Button android:id="@+id/button1"
..........
/>

<Button android:id="@+id/button2"
..........
/>

After finding there ids from xml than set the OnclickListener on it like below and do not forget to implement the OnClickListener listener on yours class like public class CLASSNAME extends Activity implements OnClickListener

 button1 .setOnClickListener(CLASSNAME.this);
 button2.setOnClickListener(CLASSNAME.this);

Than after implements the onclickListener than it will provide you a overrided method like onclick(View v)

@Override
public void onClick(View v) {

    switch (v.getId()) {
    case R.id.button1 : //Here i assumes that button1 is the name of button in yours xml which you declare button1
    //  do something on button1 click
    break;

    case R.id.button2 :////Here i assumes that button2 is the name of button in yours xml which you declare button2

    //  do something on button2 click
    break;
 }
}


回答4:

There are two ways to perform action on a Button click

  1. the first approach is to set an android:OnClick = "methodName" in the layout (xml) file in the Button code and use that method in the java code to access that Button like

    public void methodName(View v) {
        // Do what you want to perform on Button click
    }
    
  2. the second way to perform some thing behind a button is to access that button in java code and do what you want to do like

     Button button = findViewById(R.id.button);
     button.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
                // Do what you want to perform on that button click
             }
         });
    


回答5:

  • create two buttons in your activity layout and assign some id's to them like this

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button2"
        android:layout_gravity="center_horizontal|top" />
    

    Then, In your activity class file get the regerence for those buttons and set onclick Listener like below (write everything within onCreate() or onResume method). Inside onClick method do whatever you want.

      Button myButton1;
      Button myButton2;
      int a;
      protected void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
               setContentView(R.layout.activity_home);
               myButton1 = (Button) findViewById(R.id.my_button1); 
    
               myButton1.setOnClickListener( new OnClickListener() {
    
                   @Override
                   public void onClick(View v) { a =1;
               }
    
                myButton2 = (Button) findViewById(R.id.my_button1);
                myButton2.setOnClickListener( new OnClickListener() {
    
                   @Override
                   public void onClick(View v) { a =2;
               }