簡體   English   中英

在點擊按鈕以外的其他活動上顯示文本

[英]Show text on other activity than the button clicked

我知道如何在同一頁面上單擊按鈕來顯示文本,但是我的問題是(由於我在Google上找不到任何內容):單擊按鈕時,是否有可能在其他活動中顯示文本?

是的你可以

在您的FirstActivity中,單擊按鈕時執行以下操作:

Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("data","Messsage to be sent");
startActivity(intent);

在您的SecondActivity內部的onCreate()

String someData = getIntent().getStringExtra("data");
yourTextView.setText(someData);

您可以為此使用Intent Intent用於從第一個活動繼續進行其他活動。

您可以使用 :

Intent i = new Intent(MainActivity.this,SecondActivity.class);
        i.putExtra("YourValueKey", yourData.getText().toString());

那么您可以通過以下方式從第二個活動中獲取它:

Intent intent = getIntent();
String YourtransferredData = intent.getExtras().getString("YourValueKey");

如果您需要在活動之間傳遞值,請使用以下命令:

 String name="aaaa";
 Intent intent=new Intent(Main_Activity.this,Other_Activity.class);
 intent.putExtra("name", name);
 startActivity(intent);

並使用以下代碼恢復新Activity上的數據:

Bundle b = new Bundle();
b = getIntent().getExtras();
String name = b.getString("name");

暫無
暫無

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

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