首页 > 代码库 > Button的五种点击事件

Button的五种点击事件

1.内部类方式

class MyOnClickListener implements View.OnClickListener{


/**
* Called when a view has been clicked.
*
* @param v The view that was clicked.
*/
@Override
public void onClick(View v) {

}
}
        MyOnClickListener l = new MyOnClickListener();
        bt.setOnClickListener(l);

 

 

2.匿名内部类方式

Button bt = (Button) findViewById(R.id.button);

//设置监听器,采用匿名内部类方式
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//获取输入电话号码字符串
String number = et.getText().toString().trim();
Log.e("dail","click");
Log.e("dail",number);
//打电话
dailPhone(number);
}
});

3.外部类方式

 1 package market.phone;
 2 
 3 import android.view.View;
 4 
 5 /**
 6  * Created by Administrator on 2017/6/13.
 7  */
 8 
 9 public class POnClickListener implements View.OnClickListener {
10     /**
11      * Called when a view has been clicked.
12      *
13      * @param v The view that was clicked.
14      */
15     @Override
16     public void onClick(View v) {
17         
18     }
19 }
 bt.setOnClickListener(new POnClickListener());

 

4.xml方式

技术分享

    //xml配置的按钮点击后要执行的方法
    public void click(View v){

    }

5.本类实现监听接口,实现多个按钮的点击响应

技术分享

技术分享

 

Button的五种点击事件