如何在Android Studio中同时在两个活动中动态创建一个TextView?

huangapple go评论99阅读模式
英文:

How can I create dynamically a TextView simultaneously in two activities in android studio?

问题

I have two activities, in activityOne the user will dynamically create textViews and I want that in the same time to dynamically create textViews with the same content inside activityTwo.
For example:
In activityOne the user will dynamically create a textView "Groceries" I want that in the same time in activityTwo to be dynamically created a textView with the same name: "Groceries".

英文:

I have two activities, in activityOne the user will dynamically create textViews and I want that in the same time to dynamically create textViews with the same content inside activityTwo.
For example:
In activityOne the user will dynamically create a textView "Groceries" I want that in the same time in activityTwo to be dynamically created a textView with the same name: "Groceries".

答案1

得分: 0

以下是代码的中文翻译部分:

  1. 您可以使用数据仓库类如下所示
  2. public class DataRepository {
  3. private static final DataRepository INSTANCE = new DataRepository();
  4. private final MutableLiveData<String> text = new MutableLiveData<>();
  5. public void updateText(String newText){
  6. text.setValue(newText);
  7. }
  8. public MutableLiveData<String> getText() {
  9. return text;
  10. }
  11. public static DataRepository getInstance(){
  12. return INSTANCE;
  13. }
  14. }

然后,您可以使用类似以下方式的 Observer 来监听数据的变化:

  1. DataRepository dataRepository = DataRepository.getInstance();
  2. dataRepository.getText().observe(this, new Observer<String>() {
  3. @Override
  4. public void onChanged(String newText) {
  5. textView.setText(newText);
  6. }
  7. });

其中,this 指的是 Activity。

要更新文本,只需在程序的任何地方调用如下:

  1. DataRepository dataRepository = DataRepository.getInstance();
  2. dataRepository.updateText("新文本");

观察者会自动更新所有内容。

英文:

You can use a data repository class as such

  1. public class DataRepository {
  2. private static final DataRepository INSTANCE = new DataRepository();
  3. private final MutableLiveData&lt;String&gt; text = new MutableLiveData&lt;&gt;();
  4. public void updateText(String newText){
  5. text.setValue(newText);
  6. }
  7. public MutableLiveData&lt;String&gt; getText() {
  8. return text;
  9. }
  10. public static DataRepository getInstance(){
  11. return INSTANCE;
  12. }
  13. }

You can then listen to changes on the data using an Observer like so

  1. DataRepository dataRepository = DataRepository.getInstance();
  2. dataRepository.getText().observe(this, new Observer&lt;String&gt;() {
  3. @Override
  4. public void onChanged(String newText) {
  5. textView.setText(newText);
  6. }
  7. });

Where this refers to the Activity.

To update the text, simply call

  1. DataRepository dataRepository = DataRepository.getInstance();
  2. dataRepository.updateText(&quot;New text&quot;);

From anywhere in your program. The observers will update everything for you automatically.

huangapple
  • 本文由 发表于 2020年7月31日 02:56:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/63179626.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定