[英]Android: Which button was pressed
I found the following solution for detecting which of my buttons was pressed: 我发现以下解决方案来检测按下了哪个按钮:
public void onClick(View v) {
switch(v.getId())
{
case R.id.bteins:
Toast t = Toast.makeText(this, "bzeins", Toast.LENGTH_SHORT);
t.show();
break;
case R.id.btzwei:
// handle button B click;
break;
case R.id.btdrei:
Toast t1 = Toast.makeText(this, "btdfrei", Toast.LENGTH_SHORT);
t1.show();
break;
case R.id.btvier:
// handle button B click;
break;
default:
throw new RuntimeException("Unknow button ID");
}
}
My problem is that when I press bteins
or btdrei
nothing happens. 我的问题是,当我按
bteins
或btdrei
什么也没发生。 What´s my mistake? 我的错是什么?
you can implement onClick
for each button like this: 您可以为每个按钮实现
onClick
,如下所示:
findViewById(R.id.bteins).setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
//do some things!
}
});
findViewById(R.id.btzwei).setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
//do some things!
}
});
....
or implement OnClickListener
in activity and set all buttons click to that: 或在活动中实现
OnClickListener
并将所有按钮设置为此:
public class Test extends Activity implements OnClickListener
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
findViewById(R.id.bteins).setOnClickListener(this);
findViewById(R.id.btzwei).setOnClickListener(this);
findViewById(R.id.btdrei).setOnClickListener(this);
findViewById(R.id.btvier).setOnClickListener(this);
}
@Override
public void onClick(View v)
{
switch(v.getId())
{
case R.id.bteins:
Toast t = Toast.makeText(this, "bzeins", Toast.LENGTH_SHORT);
t.show();
break;
case R.id.btzwei:
// handle button B click;
break;
case R.id.btdrei:
Toast t1 = Toast.makeText(this, "btdfrei", Toast.LENGTH_SHORT);
t1.show();
break;
case R.id.btvier:
// handle button B click;
break;
default:
throw new RuntimeException("Unknow button ID");
}
}
}
Have you set OnClickListener
to the button? 您是否将
OnClickListener
设置为按钮? I guess you haven't. 我想你还没有。
public class theexample extends Activity implements OnClickListener
//OnCreateEvent
bteins.setOnClickListener(this);
btvier.setOnClickListener(this);
public void OnClick(View v){
switch(v.getId()){
case R.id.bteins:
Toast.makeText(this, "bzeins", Toast.LENGTH_SHORT).show();
t.show();
break;
case R.id.btzwei:
// handle button B click;
break;
case R.id.btdrei:
Toast.makeText(this, "btdfrei", Toast.LENGTH_SHORT).show();
break;
case R.id.btvier:
// handle button B click;
break;
default:
throw new RuntimeException("Unknow button ID");
}
}
You need to connect triggers to their respective handlers with setOnClickListener()
您需要使用
setOnClickListener()
将触发器连接到它们各自的处理程序
This method either gets this
as a parameter, and than your activity has to implement OnClickListener (Eclipse can help you auto refactor these things easily.) 该方法要么将
this
作为参数,然后您的活动就必须实现OnClickListener(Eclipse可以帮助您轻松地自动重构这些东西。)
Or, just pass it a new OnClickListener on the spot: 或者,当场将其传递给新的OnClickListener:
Button myButton = (Button) findViewById(*buttons id*);
myButton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
//You can check v attributes to see what was clicked as well.
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.