目前我的来源看起来像这样
System.out.println(text1+" "+text2); // displays the correct values.
this.view1.setText(""+text1); // should display the same values
this.view1.setText(""+text2); //
((Activity)getContext()).runOnUiThread(new Runnable()
{
public void run()
{ invalidate(); }
});
这是每30秒由另一个对象调用的方法的一部分。 TextView放置在LinearLayout上。
在开始时显示的文本是“0”。 现在我希望它每隔30秒更改一次,以显示给定的文本(例如“5”和“10”)。
看来这些观点不会重新印刷。
我希望已经说清楚了。谢谢!
答案 0 :(得分:0)
也许你的错误取决于“text1”和“text2”的错误对象类型。 这个变量是String对象还是Integer?如果您使用Integer(int)则需要 将变量值转换为String:
String str1 = String.valueOf(text1);
this.view1.setText(str1);
String str2 = String.valueOf(text2);
this.view2.setText(str2);
答案 1 :(得分:0)
设置文本不应要求您使TextView无效以进行更改。
你肯定你想要这样做吗?
this.view1.setText(""+text1); // should display the same values
this.view1.setText(""+text2); //
答案 2 :(得分:0)
实际上,Context
类没有runOnUiThread
方法,因此您需要一个Activity
对象来执行此方法。您必须在textView.setText
方法中调用runOnUiThread
方法才能使其正常工作。希望这会有所帮助。
答案 3 :(得分:0)
你可以使用handler来更新textview的值。
使用值调用处理程序。
Message m = new Message();
m.what = -1;
m.arg1 = remainTime;
handler.sendMessage(m);
******* Handler*****
public static Handler handler = new Handler() {
public void handleMessage(Message msg) {
if (msg.what == -1) {
set_timer(msg.arg1);
}
}
};
*********Updating Text***************
public static void set_timer(int time) {
if (time == 0) {
Toast.makeText(context, "Time Out...", Toast.LENGTH_SHORT).show();
System.exit(0);
} else {
if (sec < 10)
timeindex.setText(time + " : 0" + sec + " of " + value[0]);
else
timeindex.setText(time + " : " + sec + " of " + value[0]);
}
}
希望你明白......
答案 4 :(得分:0)
真正的课程看起来更复杂,但这个例子显示了我的(工作)答案,并且应该清楚它是如何工作的。
public class MyObject extends LinearLayout<br />
{
private TextView text1,text2;
private String str_text1, str_text2;
/* The constructor does the initialisation of all fields ... */
public MyObject(Context context)
{ /* Initialisation ... */ }
/*This method is called from outside by a thread to renew the displayed values.*/
public final void method_A ()
{
((Activity)getContext()).runonUIThread(
new Runnable()
{
public void run()
{
text1.setText(str_text1);
text2.setText(str_text2);
}
});
}
/* This method is called from outside by an object to initialize the two values */
method_B(String str_text1, String str_text2)
{
this.str_text1 = str_text1;
this.str_text2 = str_text2;
}
}