簡體   English   中英

單擊活動1中的按鈕,如果單擊該按鈕,則在活動3中顯示textview…

[英]click button in activity 1 and if the button clicked show textview … in activity 3

標題只是一個例子。 在我的第一個Activity我有2個按鈕。 如果您按下第一個ButtonButton1 ),您將訪問activity 2.1。 但是,當您點擊第二個ButtonButton2 )時,您將訪問Activity 2.2。 activity 3中,有必要知道用戶點擊了哪個Button (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);
}

在按鈕上,單擊開始活動並按意圖發送數據。 使用布爾標志來檢測是否單擊了第一個按鈕。

在第一個和第二個按鈕上單擊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
        }

在第一個按鈕上單擊

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

解決方案1:

您可以根據單擊的按鈕設置integer數值12 ,然后將此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中獲得button的值

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

並根據按鈕的值設置button可見性

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM