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方式
//xml配置的按钮点击后要执行的方法
public void click(View v){ }
5.本类实现监听接口,实现多个按钮的点击响应