简体   繁体   English

在点击按钮以外的其他活动上显示文本

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

我知道如何在同一页面上单击按钮来显示文本,但是我的问题是(由于我在Google上找不到任何内容):单击按钮时,是否有可能在其他活动中显示文本?

Yes, you can 是的你可以

In your FirstActivity execute this when button is clicked: 在您的FirstActivity中,单击按钮时执行以下操作:

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

In your SecondActivity inside onCreate() : 在您的SecondActivity内部的onCreate()

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

You can use Intent for this. 您可以为此使用Intent Intent is used to move on other activity from first activity. Intent用于从第一个活动继续进行其他活动。

You can use : 您可以使用 :

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

then you can get it from your second activity by : 那么您可以通过以下方式从第二个活动中获取它:

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

If you need pass values between the activities you use this: 如果您需要在活动之间传递值,请使用以下命令:

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

And this code to recovery data on new Activity: 并使用以下代码恢复新Activity上的数据:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何知道在其他活动中单击了哪个按钮 - how to know which button is clicked in other activity 单击活动1中的按钮,如果单击该按钮,则在活动3中显示textview… - click button in activity 1 and if the button clicked show textview … in activity 3 Android-单击按钮以显示具有ListView的活动时,应用程序崩溃 - Android - App crashes when button is clicked to show an activity with listview 在单击按钮后显示一个面板,在同一帧中单击第二个按钮时显示其他面板 - show one panel after button is clicked and other panel when second button is clicked in the same frame 单击第三个活动中的按钮时,第一个和第二个活动是否可能不会再次显示? - Is it possible that First and Second activity will not show again when clicked the button in third activity? Java Swing; 单击按钮后如何显示文本 - Java Swing; how to show text after button is clicked 在其他活动之上显示活动 - Show activity on top of other activity 获取点击按钮的文字? - Get text of a clicked button? 如何从 Android Studio 中的主活动以外的默认活动点击按钮打开链接? - How to Open a link on button click From a Default Activity other than Main Activity in Android Studio? 如何显示Android中TextView上的示例文本以外的值 - How to show values other than the sample text that's on textView in Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM