Java 单击活动1中的按钮,如果该按钮单击显示文本视图。。。在活动3中

Java 单击活动1中的按钮,如果该按钮单击显示文本视图。。。在活动3中,java,android,xml,button,android-activity,Java,Android,Xml,Button,Android Activity,标题只是一个例子。在我的第一个活动中有两个按钮。如果您点击第一个按钮(按钮1),您将访问活动2.1。但当您点击第二个按钮(按钮2)时,您将访问活动2.2。在活动3中,有必要知道用户点击了哪个按钮(按钮在此处输入代码2或1),因为要显示的文本视图不同。那么,如何显示不同的文本视图 此代码位于活动中。3。 public void Button1 (View view) { TextView txtView = (TextView)findViewById(R.id.nextText);

标题只是一个例子。在我的第一个
活动中
有两个按钮。如果您点击第一个
按钮(
按钮1
),您将访问
活动
2.1。但当您点击第二个
按钮(
按钮2
)时,您将访问
活动
2.2。在
活动
3中,有必要知道用户点击了哪个
按钮
(按钮
在此处输入代码
2或1),因为要显示的
文本视图不同。那么,如何显示不同的
文本视图

此代码位于活动中。3。

 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(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);
点击第二个按钮

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();}
在textview中使用标记android:tag=“1”作为第一个按钮,android:tag=“2”作为第二个按钮。如果用户单击按钮,则使用txtView.getTag()获取标记,在第三个活动中,使用Intent Intent=new Intent()发送标记;
intent.putExtra(“标记”,此处标记);
星触觉(……);

并在第三个活动中使用String value=getIntent().getExtras().getString(“标记”)在按钮上单击开始活动并发送意图中的数据。 使用布尔标志检测是否单击了第一个按钮

在第一个和第二个按钮上,单击“在意图中传递布尔标志”

 Intent intent = new Intent(FirstActivity.this, ThirdActivity.class);
                intent.putExtra("BUTTON_1_CLICKED", true);
                startActivity(intent);
在第三个活动中,使用Bundle获得单击按钮标志

if (extras != null) {
            Boolean value = extras.getBoolean("BUTTON_1_CLICKED", false);
           activity
        }

解决方案1:

您可以根据单击的按钮设置
整数
1
2
,然后将此
整数
从活动1传递到活动2,然后从活动2传递到活动3,并检入活动3并根据
整数
值设置可见性

示例:

在按钮1上,单击:

Intent i = new Intent(this, ActivityTwo.class);
i.putExtra("button", 1);
startActivity(i);
editor.putInt("button", 1);
editor.apply(); 
在按钮2上,单击:

Intent i = new Intent(this, ActivityTwo.class);
i.putExtra("button", 2);
startActivity(i);
editor.putInt("button", 2); 
editor.apply(); 
并在活动2的
onCreate
方法中获取该值:

Bundle b = getIntent().getExtras();
if (b != null) {
    int button = b.getInt("button");
}
然后再次将该值传递给活动3,这样您就可以知道在收到的
整数的帮助下单击了哪个按钮

解决方案2:

单击按钮可将按钮编号
integer
值保存在
SharedReferences
中,并在活动3中检索该值,并根据
integer
值设置按钮的可见性

示例:

在活动1中声明
编辑器

SharedPreferences.Editor editor;
在活动的
onCreate
方法中初始化
编辑器

editor = getSharedPreferences("myPrefs", Context.MODE_PRIVATE).edit();
sharedPreferences = getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
然后在按钮1上单击:

Intent i = new Intent(this, ActivityTwo.class);
i.putExtra("button", 1);
startActivity(i);
editor.putInt("button", 1);
editor.apply(); 
然后在按钮2上单击:

Intent i = new Intent(this, ActivityTwo.class);
i.putExtra("button", 2);
startActivity(i);
editor.putInt("button", 2); 
editor.apply(); 
现在在活动3中声明
SharedReferences

SharedPreferences sharedPreferences;
在活动的
onCreate
方法中初始化
SharedReferences

editor = getSharedPreferences("myPrefs", Context.MODE_PRIVATE).edit();
sharedPreferences = getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
在活动3中,获取按钮的值

int button = sharedPreferences.getInt("button", 0);

并根据
按钮的值设置按钮的可见性

使用
意图将按钮名称发送为
putStringExtra
,但现在如何显示同一活动上的文本视图?textViews1.setVisibility(View.VISIBLE);和textViews2.setVisibility(View.GONE);设置要显示的“可见”,并设置要隐藏的“消失”(button_name.equalsIgnoreCase(“button1”){//do here for button 1 from activity 1}如果(button_name.equalsIgnoreCase(“button2”){//do here for button 2 from activity 2.2}}在这种情况下,这是我的第一个想法,但我想知道button的SharedReferences是如何工作的。我很高兴你发现这个答案很有用并接受了这个答案。我还有一个问题。。对此我很抱歉,但如何将visible accoird设置为按钮的值?在我看来,它是与if(按钮…)一起工作的,但它不是。