如何将按钮 ID 传递给其他 Activity? Android

How can I pass an Button ID to other Activity? Android

我有 2 个活动,我的第一个 activity 有 3 个不同 ID 的按钮,我怎样才能将点击按钮的 ID 传递给第二个 activity?

**
我有 3 种不同类型的搜索,第二种 activity 它是一张地图,如果单击按钮 1 将显示 X 个结果,如果单击按钮 2 将显示 Y 个结果,如果单击 Buttom 3 将显示 N 个结果,我正在考虑使用按钮 ID 在地图上制作 IF,这样我就可以通过用户点击的按钮进行检查**

在按钮上使用 getId() 单击 returns 一个整数。您可以将其保存在 SharedPreferences 中或通过 Bundle.

发送

通过Intent

完成
Button myButton = <GetYourButton>;
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("FirstButtonID", myButton.getId());
startActivity(intent);

在第二个Activity里检索onCreate

Intent intent = getIntent();
int buttonId = intent.getIntExtra("FirstButtonID", <DefaultButtonIDIncaseNoneIsPassed>);

这样传

Intent intent = new Intent(CurrentActivity.this, NewActivity.class);
intent.putExtra("intVariableName", v.getId()); //where v is button that is cliked, you will find it as a parameter to onClick method
startActivity(myIntent);

像这样检索

Intent mIntent = getIntent();
int intValue = mIntent.getIntExtra("intVariableName", 0);
if(intValue == 0)
    // error handling (Will come in this if when button id is invalid)
else
{
   if(intValue == R.id.button1)
       // Do work related to button 1

   if(intValue == R.id.button2)
       // Do work related to button 2

   if(intValue == R.id.button3)
       // Do work related to button 3
}