简体   繁体   English

显示其他文件中的文本,没有mainActivity

[英]display text from other file no the mainActivity

Is there a way to write to the screen from another file other than the mainActivity file? 是否可以从mainActivity文件以外的其他文件写入屏幕? I can write the the screen in my mainActivity as follows. 我可以在mainActivity中编写屏幕,如下所示。

MainActivity.java MainActivity.java

TextView myTextViewTime= (TextView)findViewById(R.id.connected);
myTextViewTime.setText("Hello");

but I can't do this from say a file called hell.java 但是我不能说一个叫hell.java的文件

  String msg = "hello";
  public void  message_recieve(String msg){
      TextView myTextViewTime= (TextView)findViewById(R.id.connected);
      myTextViewTime.setText("Hello");
   }

You can do so if you pass the other class a context of your activity. 如果您向另一个类传递活动的上下文,则可以这样做。 You can also for communication to occur on the UIThread with the setting of text via a 您也可以通过以下方式使用文本设置在UIThread上进行通信:

runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                               myTextViewTime.setText("Hello");
                            }
                     });

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM