Android Button的四种点击事件
bta1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(ButtonActivity1.this, "匿名类", Toast.LENGTH_SHORT).show();
}
});
bta2.setOnClickListener(new Clicker());
public class Clicker implements View.OnClickListener {
@Override
public void onClick(View v) {
Toast.makeText(ButtonActivity1.this, "内部类", Toast.LENGTH_SHORT).show();
}
}
第三种
因为继承了 View.OnClickListener接口,所以要重写onclick方法
public void onClick(View v) {
Toast.makeText(ButtonActivity1.this, "使用Activity本类", Toast.LENGTH_SHORT).show();
}
bta4.setOnClickListener(new OutputClick(this));
//外部类
public class OutputClick implements View.OnClickListener {
private Context context;
public OutputClick(Context context){
this.context=context;
}
@Override
public void onClick(View v) {
Toast.makeText(context , "外部类", Toast.LENGTH_SHORT).show();
}
}