android中多个按钮用一个监听器监听怎么写,求具体的代码,谢谢。 15
2个回答
展开全部
1、先申明一个监听对象
2、不同按钮设置同一个监听对象
3、监听中,通过按钮的id判断是哪个按钮的点击事件
代码如下
1、new监听
OnClickListener clickListener = new OnClickListener(){
@Override
public void onClick(View v) {
if (v.getId()==R.id.button1){//按钮1
}else if(v.getId()==R.id.button2}{//按钮2
}
}
2、设置监听
Button btn1 = (Button)findViewById(R.id.button1);
Button btn2 = (Button)findViewById(R.id.button1);
btn1.setOnClickListener(clickListener);
btn2.setOnClickListener(clickListener);
2、不同按钮设置同一个监听对象
3、监听中,通过按钮的id判断是哪个按钮的点击事件
代码如下
1、new监听
OnClickListener clickListener = new OnClickListener(){
@Override
public void onClick(View v) {
if (v.getId()==R.id.button1){//按钮1
}else if(v.getId()==R.id.button2}{//按钮2
}
}
2、设置监听
Button btn1 = (Button)findViewById(R.id.button1);
Button btn2 = (Button)findViewById(R.id.button1);
btn1.setOnClickListener(clickListener);
btn2.setOnClickListener(clickListener);
展开全部
public static class PlaceholderFragment extends Fragment implements
OnClickListener {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
button1 = (Button) rootView.findViewById(R.id.button1);
button2 = (Button) rootView.findViewById(R.id.button2);
button3 = (Button) rootView.findViewById(R.id.button3);
button4 = (Button) rootView.findViewById(R.id.button4);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
button4.setOnClickListener(this);
return rootView;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
Toast.makeText(getActivity(), "这是BUTTON1", Toast.LENGTH_SHORT).show();
break;
case R.id.button2:
Toast.makeText(getActivity(), "这是BUTTON2", Toast.LENGTH_SHORT).show();
break;
case R.id.button3:
Toast.makeText(getActivity(), "这是BUTTON3", Toast.LENGTH_SHORT).show();
break;
case R.id.button4:
Toast.makeText(getActivity(), "这是BUTTON4", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
}
OnClickListener {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
button1 = (Button) rootView.findViewById(R.id.button1);
button2 = (Button) rootView.findViewById(R.id.button2);
button3 = (Button) rootView.findViewById(R.id.button3);
button4 = (Button) rootView.findViewById(R.id.button4);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
button4.setOnClickListener(this);
return rootView;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
Toast.makeText(getActivity(), "这是BUTTON1", Toast.LENGTH_SHORT).show();
break;
case R.id.button2:
Toast.makeText(getActivity(), "这是BUTTON2", Toast.LENGTH_SHORT).show();
break;
case R.id.button3:
Toast.makeText(getActivity(), "这是BUTTON3", Toast.LENGTH_SHORT).show();
break;
case R.id.button4:
Toast.makeText(getActivity(), "这是BUTTON4", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
}
追问
这个要怎么用呢?不是有一种方法
OnClickListener clickListener = new OnClickListener(){
@Override
public void onClick(View v) {
}
}
然后onClick函数里就可以通过switch来判断是哪个按钮,但我不清楚OnClickListener函数要怎么用,是直接这么写还是需要被调用,总是得不到预期的效果。
追答
把上面this换成你的clickListener activity也不需要实现onclicklistener接口
其它是跟我上面一样
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询