简体   繁体   English

如何在从另一个活动传递的字符串中添加文本并在 Textview 中显示结果文本

[英]How to add the text in String passed from another activity and show the resulting text in Textview

I am passing value from Activity A to Activity B .我正在将值从Activity A传递到Activity B It works fine.它工作正常。

For Example, if I pass String "Harvey Brown" , Then in next activity it also shows "Harvey Brown" .例如,如果我传递 String "Harvey Brown" ,那么在下一个活动中它也会显示"Harvey Brown"

But I would also like to add "Author: " text before "Harvey Brown" and show the sum result in TextView such that now instead of only "Harvey Brown" .但我还想在"Harvey Brown"之前添加"Author: "文本并在TextView显示总和结果,这样现在而不是只有"Harvey Brown"

It will show它会显示

Author:Harvey Brown

for every intent.对于每个意图。

Here is the code, I'm using to pass String value:这是代码,我用来传递字符串值:

  intent.putExtra("Title",mData.get(position).getTitle());

and in next activity I received value by在接下来的活动中,我收到了价值

Intent intent = getIntent();


    String author_name = intent.getExtras().getString("author_name");

and I set the value accordingly as我相应地将值设置为

 author_name = (TextView) findViewById(R.id.author_nameid);
 author_name.setText(author_name);

Solution:解决方案:

Just change this line:只需更改这一行:

intent.putExtra("Title",mData.get(position).getTitle());

To

intent.putExtra("Title", "Author: " + mData.get(position).getTitle());

You will get your desired result.你会得到你想要的结果。

You can add a 'Author:' Prefix like below -您可以添加“作者:”前缀,如下所示 -

author_name = (TextView) findViewById(R.id.author_nameid); 
author_name.setText("Author: " + author_name);

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

相关问题 从另一个活动获取textView的文本 - Get text of textView from another activity 如何从Textview的getText()中添加预定义的文本并显示结果 - How to add pre defined text in getText() from Textview and show the result 使用另一个类的字符串设置TextView文本 - Set TextView text with string from another class 从另一个活动设置ImageView资源和TextView文本 - Set ImageView Resource and TextView Text from Another Activity 如何从用户输入的编辑文本中获取信息并将其作为TextView移至另一个活动 - How to get information from an edit text that is entered by the user and move it to another activity as a TextView 如何在android studio中点击按钮时从另一个活动更改textview文本? - How to change textview text from another activity on button click in android studio? 如何将两个不同的文本意图从一个活动发送到另一个活动到同一个 textview? - How can I send two different text intents to the same textview from one activity to another? 如何通过从userinput(edittext)获取文本并将其放入另一个活动的textview中? - How to pass get text from userinput (edittext) and put into a textview in another activity? 将文本从EditText转换为字符串以进行其他活动 - Get text from EditText into string to another activity 如何从Loader向TextView添加文本 - How to add text to TextView from Loader
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM