您可以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!
}
});
....
或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");
}
}
}