Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/187.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android有许多按钮_Android_Button_Android Intent_Android Activity - Fatal编程技术网

Android有许多按钮

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

我在尝试一件事:在我的活动中,我有30个按钮,每个人都有一个从1到30的数字。现在,我想为每个人分配一个意图,该意图打开相同的活动,但传递相应的编号。我该怎么办

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);

}

完美的我不知道标签的属性