单击活动1中的按钮,如果单击按钮,则在活动3中显示textview ....

时间:2017-07-04 09:15:43

标签: java android xml button android-activity

标题只是一个例子。在我的第一个Activity我有2个按钮。如果您点击第一个ButtonButton1),您将访问activity 2.1。但是,当您点击第二个ButtonButton2)时,您将访问Activity 2.2。在activity 3中,必须知道用户点击了哪个Button(按钮enter code here 2或1),因为它们将显示不同的TextViews。那么如何展示这种不同的TextViews

此代码位于Activity 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);
}

4 个答案:

答案 0 :(得分:1)

在按钮上单击开始活动并以意图发送数据。 使用布尔标志来检测是否单击了第一个按钮。

在第一个和第二个按钮上单击传递Intent中的布尔标志

 Intent intent = new Intent(FirstActivity.this, ThirdActivity.class);
                intent.putExtra("BUTTON_1_CLICKED", true);
                startActivity(intent);

在你的第3个活动中,使用Bundle

获得点击按钮标志
if (extras != null) {
            Boolean value = extras.getBoolean("BUTTON_1_CLICKED", false);
           activity
        }

答案 1 :(得分:0)

点击第一个按钮

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

答案 2 :(得分:0)

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

答案 3 :(得分:0)

解决方案1:

您可以根据点击的按钮设置integer12,然后将此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);

并在活动2的onCreate方法中获取此值:

Bundle b = getIntent().getExtras();
if (b != null) {
    int button = b.getInt("button");
}

再次将此值传递给活动3,这样您就可以在收到的integer的帮助下了解点击了哪个按钮。

解决方案2:

您可以在点击按钮上保存integer中的按钮编号SharedPreferences值,并在活动3中检索该值,并根据integer值设置按钮的可见性。< / p>

示例:

在您的活动1中声明editor

SharedPreferences.Editor editor;

在您的活动的editor方法中初始化onCreate

editor = getSharedPreferences("myPrefs", Context.MODE_PRIVATE).edit();

在按钮1上单击:

editor.putInt("button", 1);
editor.apply(); 

在按钮2上单击:

editor.putInt("button", 2); 
editor.apply(); 

现在在您的活动3中声明sharedPreferences

SharedPreferences sharedPreferences;

在您的活动的sharedPreferences方法中初始化onCreate

sharedPreferences = getSharedPreferences("myPrefs", Context.MODE_PRIVATE);

在活动3中获取按钮的值

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

并根据button

的值设置按钮的可见性