Android有许多按钮
我在尝试一件事:在我的活动中,我有30个按钮,每个人都有一个从1到30的数字。现在,我想为每个人分配一个意图,该意图打开相同的活动,但传递相应的编号。我该怎么办Android有许多按钮,android,button,android-intent,android-activity,Android,Button,Android Intent,Android Activity,我在尝试一件事:在我的活动中,我有30个按钮,每个人都有一个从1到30的数字。现在,我想为每个人分配一个意图,该意图打开相同的活动,但传递相应的编号。我该怎么办 public void ApriTavolo(View v) { Bundle extras = new Bundle(); // pass the value of button extras.putString("one", one); // Perform acti
public void ApriTavolo(View v) {
Bundle extras = new Bundle();
// pass the value of button
extras.putString("one", one);
// Perform action on click
Intent activityChangeIntent = new Intent(MainActivity.this, Interno_tavolo.class);
activityChangeIntent.putExtras(extras);
startActivity(activityChangeIntent);
}
你可以这样做
String pressed = null;
switch(v.getId()) {
case R.id.firstButton:
pressed = "one";
break;
// up to the end
}
Intent activityChangeIntent = new Intent(MainActivity.this, Interno_tavolo.class);
extras.putString("one", one);
activityChangeIntent.putExtras(extras);
startActivity(activityChangeIntent);
或者您可以为每个按钮分配一个标签(有属性android:tag
);您可以通过以下方式检索它:
Intent activityChangeIntent = new Intent(MainActivity.this, Interno_tavolo.class);
extras.putString("one", (String)v.getTag());
activityChangeIntent.putExtras(extras);
startActivity(activityChangeIntent)
如果按钮中只有数字,请确保所有操作都使用相同的侦听器方法 获取文本并将文本作为参数发送
public void ApriTavolo(View v) {
Bundle extras = new Bundle();
Button b = (Button) v;
String value = b.getText().toString();
// pass the value of button
extras.putString("value", value);
// Perform action on click
Intent activityChangeIntent = new Intent(MainActivity.this, Interno_tavolo.class);
activityChangeIntent.putExtras(extras);
startActivity(activityChangeIntent);
}
完美的我不知道标签的属性