directory

1. Specify the onclick() method in XML

2, NEW an OnClickListenner() interface implementation

3, implement OnClickListener interface (Switch method)


Hello, I’m Grey Ape! A super bug writing program ape!

Here today, we summarize and record the three implementation methods of button click event response in Android development. These three methods are respectively:

Specify methods for onclick() in XML;

In Actitivy, new an OnClickListenner();

Implement the OnClickListener interface

Let’s take a look at each of these three methods:

 

1. Specify the onclick() method in XML

Define the properties of the button button below the XML file, specifying the method name for the onclick property, as in this case the method name is “btn_1”.

    <Button
        android:id="@+id/btn_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="btn_1"
        android:text="+" />
Copy the code

The btn_1() method that binds the button is then implemented in MainActivity

   public void btn_1(View v)
    {
        // Bind the btn_1 method
        Toast.makeText(getApplicationContext(), "Bind method 1", Toast.LENGTH_LONG).show();
    }
Copy the code

 

2, NEW an OnClickListenner() interface implementation

Method one:

The button control is associated in the default onCreate() method in the mainActivity.java file, and then an implementation interface is added to the associated control to write the response method

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
      
        // Step 1: Associate the control
        Button btn_add= findViewById(R.id.btn_add);

        // Step 2: Instantiate the interface
        btn_add.setOnClickListener(new View.OnClickListener() {
          
            @Override
            public void onClick(View v) {
                // Write the response method to it}}); }Copy the code

Method 2:

An alternative way to use the OnClickListenner() interface to implement the event response is to implement the OnClickListenner() interface separately and then add the interface methods to the control, which makes the code more concise and intuitive.

A note of caution here: willInterface implementation methods bind to the code of the controlIt should be written last, ensuring that **** implements the interface method before binding the control

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
      
        // Step 1: Associate the control
        Button bt_1= findViewById(R.id.btn_add);

        // Step 2: Implement the interface
        View.OnClickListener add = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Respond to events}};// Step 3: Interface binding control
        bt1.setOnClickListener(add);
    }
Copy the code

 

3, implement OnClickListener interface (Switch method)

First, the class references the OnClickListener interface and implements the method

public class MainActivity extends Activity implements View.OnClickListener{
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        // Associate the control
        btn_add=(Button) findViewById(R.id.btn_add);
        btn_reduce=(Button) findViewById(R.id.btn_reduce);
        et=(EditText) findViewById(R.id.editText1);

        // Step 2, use the interface
        btn_add.setOnClickListener(this);
        btn_reduce.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch(v.getId())
        {
        case R.id.btn_add:   
            // The response time of the corresponding control
            break;
        case R.id.btn_reduce:   
            // The response time of the corresponding control
            break; }}}Copy the code

There are three ways for buttons to respond to events,

Feel good remember to like attention yo!