【问题标题】:click button in activity 1 and if the button clicked show textview ... in activity 3单击活动 1 中的按钮,如果单击按钮,则在活动 3 中显示 textview ...
【发布时间】:2017-12-07 16:24:03
【问题描述】:

标题只是一个例子。在我的第一个 Activity 中,我有 2 个按钮。如果您点击第一个Button (Button1),您将访问activity 2.1。但是当您点击第二个Button(Button2) 时,您将访问Activity 2.2。在activity 3 中,有必要知道用户点击了哪个Button(按钮enter code here2 或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);
}

【问题讨论】:

  • 将按钮名称发送为putStringExtraIntent

标签: java android xml button android-activity


【解决方案1】:

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

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

 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
        }

【讨论】:

    【解决方案2】:

    解决方案 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 值设置按钮的可见性。

    示例:

    在您的活动 1 中声明 editor

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

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

    SharedPreferences sharedPreferences;
    

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

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

    并在活动3中获取按钮的值

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

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

    【讨论】:

    • 嗯,这是我的第一想法,但我知道 SharedPreferences 和按钮是如何工作的。
    • 很高兴您发现此答案有用并接受了此答案。
    • 我还有一个问题.. 对此我很抱歉,但是如何将可见的 accoird 设置为按钮的值?在我看来,它与 if (button..) 一起工作,但事实并非如此。
    【解决方案3】:

    第一次点击按钮

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

    【讨论】:

    • Ty 但是我现在如何在同一个 Activity 上显示 textViews?
    • textViews1.setVisibility(View.VISIBLE);和 textViews2.setVisibility(View.GONE);
    • 设置要显示的“Visible”并设置要隐藏的“gone”
    • if(button_name.equalsIgnoreCase("button1")){ //从活动 1 中为按钮 1 执行此操作 } if(button_name.equalsIgnoreCase("button2")){ //为按钮 2 执行此操作从活动 2.2 } }
    • 在这种情况下
    【解决方案4】:

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

    【讨论】:

      猜你喜欢
      • 2014-10-17
      • 2015-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多