android里点击监听实现的方法中的view.getid()的作用
2个回答
展开全部
当很多个控件注册同一个监听的时候,就是通过view.getid()返回的id来区别究竟是哪一个控件响应了监听事件.
Button btn1,btn2;
btn1 = (Button)findViewById(R.id.btn1);
btn2 = (Button)findViewById(R.id.btn2);
View.OnclickListener listener = new OnclickListener(){
@Override
public void onClick( View view){
switch(view.getId()){
case R.id.btn1:
Log.d("btn1"," has been clicked! ");
break;
case R.id.btn2:
Log.d("btn2","has been clicked !");
break;
default:
break;
}
}
}
btn1.setOnclickListener(listener);
btn2.setOnclickListener(listener);
Button btn1,btn2;
btn1 = (Button)findViewById(R.id.btn1);
btn2 = (Button)findViewById(R.id.btn2);
View.OnclickListener listener = new OnclickListener(){
@Override
public void onClick( View view){
switch(view.getId()){
case R.id.btn1:
Log.d("btn1"," has been clicked! ");
break;
case R.id.btn2:
Log.d("btn2","has been clicked !");
break;
default:
break;
}
}
}
btn1.setOnclickListener(listener);
btn2.setOnclickListener(listener);
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询