我正在尝试一件事:在我的Activity中我有30个按钮,每个人都有一个从1到30的数字。现在我想为每个分配一个打开相同Activity但传递相应数字的Intent 。我该怎么办?
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);
}
答案 0 :(得分:1)
如果您只有按钮编号,请确保所有内容都采用相同的侦听方法
并获取文本并将文本作为参数发送。
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);
}
答案 1 :(得分:0)
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)