public void onClick(View v)
{
if (v.getId()== R.id.but1 && v.getId()== R.id.but2)
{
Intent intent=new Intent(First.this,Second.class);
startActivity(intent);
}
}
答案 0 :(得分:1)
没有可以与两个控件关联的此类事件。事件处理程序仅与一个控件关联,而不同于将同一个侦听器分配给两个按钮。听众将分别从每个按钮接听电话。
此外,侦听器永远不会被一起触发,因为它们都在同一个线程(UI线程)中运行。在某个时刻,无法捕获两个控件的click事件。一个听众将被触发,然后另一个。即使我们假设用户设法在完美世界中以相同的毫秒左右一起点击它们。任何方式谁可以决定,当他们被点击相同的毫秒时,他们认为被点击收集!为什么不是同样的纳秒。为什么不是同一个小时:)
好的,这足以解释点击事件。
我们需要的是触摸事件,它可以如下播放(代码也将解释触摸事件如何工作):
活动类成员:
public boolean b1Down = false, b2Down = false;
onCreate方法代码:
Button b1 = (Button)findViewById(R.id.button1);
Button b2 = (Button)findViewById(R.id.button2);
b1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
boolean consume = false;
if (event.getAction() == MotionEvent.ACTION_UP)
{
b1Down = false;
}
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
b1Down = true;
if (b2Down)
{
// both are clicked now //
Toast.makeText(MainActivity.this, "Both are clicked now!", Toast.LENGTH_SHORT).show();
}
consume = true;
}
return consume;
}
});
b2.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
boolean consume = false;
if (event.getAction() == MotionEvent.ACTION_UP)
{
b2Down = false;
}
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
b2Down = true;
if (b1Down)
{
// both are clicked now //
Toast.makeText(MainActivity.this, "Both are clicked now!", Toast.LENGTH_SHORT).show();
}
consume = true;
}
return consume;
}
});