public class MainActivity extends Activity implements OnClickListener{ @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//点击方式1
Button but1=(Button)findViewById(R.id.button1);
but1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(MainActivity.this, "点击1事件", 800).show();
}
});
//点击方式2
Button but2=(Button)findViewById(R.id.button2);
but2.setOnClickListener(new MyOnClickListener()); //点击方式3 类上面实现OnClickListener
Button but2=(Button)findViewById(R.id.button3);
but2.setOnClickListener(this);
}
//点击方式4
public void onClickMethod(View v){
Toast.makeText(this, "显示4", 800).show();
} class MyOnClickListener implements OnClickListener{
@Override
public void onClick(View arg0) {
Toast.makeText(MainActivity.this,"点击2事件", 800).show();
} }
//重写onClick
@Override
public void onClick(View arg0) {
Toast.makeText(this, "点击3事件", 800).show();
} }
————————————————
版权声明:本文为CSDN博主「达帮主」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_35427437/article/details/79301949