Button的五种点击事件

时间:2021-01-02 08:32:46

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.外部类方式

 package market.phone;

 import android.view.View;

 /**
* Created by Administrator on 2017/6/13.
*/ public class POnClickListener implements View.OnClickListener {
/**
* Called when a view has been clicked.
*
* @param v The view that was clicked.
*/
@Override
public void onClick(View v) { }
}
 bt.setOnClickListener(new POnClickListener());

4.xml方式

Button的五种点击事件

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

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

Button的五种点击事件

Button的五种点击事件