标题只是一个例子。在我的第一个Activity
中,我有 2 个按钮。如果您点击第一个Button
(Button1
(,您将访问activity
2.1。但是当你点击第二个Button
(Button2
(时,你将访问Activity
2.2。在第 3activity
中,有必要知道用户点击了哪个Button
(按钮enter code here
2 或 1(,因为它们将显示不同的TextViews
。那么如何展示这种不同的TextViews
呢?
此代码位于第 3Activity
。
public void Button1 (View view) {
TextView txtView = (TextView)findViewById(R.id.nextText);
if (txtView .getVisibility() == View.VISIBLE)
txtView.setVisibility(View.VISIBLE);
else
txtView.setVisibility(View.VISIBLE);
}
public void Button2 (View view) {
TextView txtView = (TextView)findViewById(R.id.nextText2);
if (txtView .getVisibility() == View.VISIBLE)
txtView.setVisibility(View.VISIBLE);
else
txtView.setVisibility(View.VISIBLE);
}
在按钮上单击启动活动并按意图发送数据。 使用布尔标志检测是否单击了第一个按钮。
在第一个和第二个按钮上,单击在意向中传递布尔标志
Intent intent = new Intent(FirstActivity.this, ThirdActivity.class);
intent.putExtra("BUTTON_1_CLICKED", true);
startActivity(intent);
在您的第三个活动中,使用捆绑包点击按钮标志
if (extras != null) {
Boolean value = extras.getBoolean("BUTTON_1_CLICKED", false);
activity
}
解决方案 1:
您可以根据单击的按钮将integer
值设置为1
或2
,然后将此integer
从活动 1 传递到活动 2,然后将活动 2 传递到活动 3,然后签入活动 3 并根据integer
值设置可见性。
例:
在按钮 1 上单击:
Intent i = new Intent(this, ActivityTwo.class);
i.putExtra("button", 1);
startActivity(i);
在按钮 2 上单击:
Intent i = new Intent(this, ActivityTwo.class);
i.putExtra("button", 2);
startActivity(i);
并在活动 2onCreate
方法中获取此值:
Bundle b = getIntent().getExtras();
if (b != null) {
int button = b.getInt("button");
}
再次将此值传递给活动 3,以便您知道在收到的integer
的帮助下单击了哪个按钮。
解决方案 2:
单击按钮时,可以在SharedPreferences
中保存按钮编号integer
值,并在活动 3 中检索该值,并根据integer
值设置按钮的可见性。
例:
在活动中声明editor
1
SharedPreferences.Editor editor;
在活动的onCreate
方法中初始化editor
。
editor = getSharedPreferences("myPrefs", Context.MODE_PRIVATE).edit();
在按钮 1 上单击:
editor.putInt("button", 1);
editor.apply();
在按钮 2 上单击:
editor.putInt("button", 2);
editor.apply();
现在在您的活动中声明sharedPreferences
3
SharedPreferences sharedPreferences;
在活动的onCreate
方法中初始化sharedPreferences
。
sharedPreferences = getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
在活动 3 中获取按钮的值
int button = sharedPreferences.getInt("button", 0);
并根据button
的值设置按钮的可见性
在第一个按钮上单击
Intent intent=new Intent(CurrentActivityName.this, CurrentActivityName3.class);
intent.putExtra("button_name","button1");
startActivity(intent);
在第二个按钮上单击
Intent intent=new Intent(CurrentActivityName.this, CurrentActivityName3.class);
intent.putExtra("button_name","button2");
startActivity(intent);
在第三个活动中:
try {
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
if (bundle.containsKey("button_name")) {
String button_name = bundle.getString("button_name");
if(button_name.equalsIgnoreCase("button1")){
//do here for button 1 from activity 1
}
if(button_name.equalsIgnoreCase("button2")){
//do here for button 2 from Activity 2.2
}
}
}
}catch (Exception e){e.printStackTrace();}
use a tag in textview android:Tag="1" for first button and android:Tag="2" for second .if user click on the button get the tag using txtView.getTag() and , in third activity sent the tag using Intent intent=new Intent();
intent.putExtra("tag",tagHere);
startActivity(....);
and receive it there in third activity using String value = getIntent().getExtras().getString("tag");