Android从另一个类更改TextView文本 [英] Android Change TextView text from another class
本文介绍了Android从另一个类更改TextView文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 MainActivity
中有 TextView
,我想更改 TextView
另一个类中的文本。
I have a TextView
in MainActivity
, I would like to change the TextView
text within another class.
如何在 TextView
>另一个类的MainActivity ?
How can I access TextView
in MainActivity
from another class?
我尝试了以下
TextView textView = (TextView)findViewById(R.id.myTextView);
TextView textView = (TextView) findViewById(R.id.myTextView);
textView.setText(Text);
textView.setText("Text");
但是当调用 setText()
推荐答案
时,应用程序崩溃了使用 runOnUiThread(new Runnable()...
You have to use runOnUiThread(new Runnable()...
请参阅以下内容:
import android.content.Context;
private class AnotherClass {
protected MainActivity context;
public AnotherClass(Context context){
this.context = (MainActivity) context;
}
public void updateTV(final String str1){
context.runOnUiThread(new Runnable() {
@Override
public void run() {
context.textView.setText(str1);
}
});
}
}
这篇关于Android从另一个类更改TextView文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文