Three kinds of click event of the Button control

① method specified onClick attribute in the layout file set click event

 

 

 

 

 

 

 

② the use of anonymous inner class method to set the click event

 

 

 

 

 

 

③ achieve Activity realize OnClickListen interface disposed click event

 

 

 

 

 

 

linear.xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/btn_one"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="click"
        android:text="按钮1"
        />
    <Button
        android:id="@+id/btn_two"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="按钮2"
        />
    <Button
        android:id="@+id/btn_three"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="按钮3"
        />

</LinearLayout>

MainActivity Code

package com.iang.buttonclick;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    Button  btn_one,btn_two,btn_three;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.linear);
        btn_one=(Button)findViewById(R.id.btn_one);
       btn_two= );(The Button) findViewById (R.id.btn_two); 
       btn_three = (the Button) findViewById (R.id.btn_three); 

//        call the anonymous class to listen for mouse click events 
       btn_two.setOnClickListener ( new new View.OnClickListener () { 
           @Override 
           public  void onClick (View v) { 
               btn_two.setText ( "2 button has been clicked" ); 
           } 
       }); 

       btn_three.setOnClickListener ( the this ); 
    } 
//     to listen for click events defined in the xml file by the click 
    public  void the click (View View) { 
       btn_one.setText ( "button has been clicked 1"
    } 

//     to listen for mouse click events by defining an interface method 
    @Override
     public  void the onClick (View V) { 
        btn_three.setText ( "button has been clicked 3" ); 
    } 
}

 

Guess you like

Origin www.cnblogs.com/PerZhu/p/11599393.html