[英]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.