Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何使TextView在运行时更改文本?_Android_Textview_Repaint - Fatal编程技术网

Android 如何使TextView在运行时更改文本?

Android 如何使TextView在运行时更改文本?,android,textview,repaint,Android,Textview,Repaint,目前我的资料来源是这样的 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()

目前我的资料来源是这样的

    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秒调用一次的方法的一部分。 文本视图放置在线性布局上

开始时显示的文本为“0”。 现在我希望它每30秒更改一次,以显示给定的文本(例如“5”和“10”)

这些视图似乎没有重新绘制


我希望已经说清楚了。谢谢

可能您的错误取决于“text1”和“text2”的错误对象类型。 这个变量是字符串对象还是整数?如果使用整数(int),则需要 要将变量值转换为字符串,请执行以下操作:

String str1 = String.valueOf(text1);
this.view1.setText(str1);

String str2 = String.valueOf(text2);
this.view2.setText(str2);

设置文本不应要求您使TextView无效以进行更改

你确定要这样做吗

this.view1.setText(""+text1); // should display the same values
this.view1.setText(""+text2); //

实际上,
Context
类没有
runOnUiThread
方法,因此需要一个
Activity
对象来执行此方法。您必须在
runOnUiThread
方法中调用
textView.setText
方法才能使其工作。希望这有帮助。

u可以使用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]);
                }

            }
Message m=新消息();
m、 什么=-1;
m、 arg1=剩余时间;
handler.sendMessage(m);
*******处理程序******
公共静态处理程序=新处理程序(){
公共无效handleMessage(消息消息消息){
如果(msg.what==-1){
设置定时器(消息arg1);
}
}
};
*********更新文本***************
公共静态无效设置\计时器(整数时间){
如果(时间==0){
Toast.makeText(上下文,“超时…”,Toast.LENGTH_SHORT.show();
系统出口(0);
}否则{
如果(第10节)
timeindex.setText(时间+“:0”+秒+“+值[0]);
其他的
timeindex.setText(时间+“:“+sec+”,共“+value[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;
    }
}
公共类MyObject扩展了LinearLayout
{ 私有文本视图文本1、文本2; 私有字符串str_text1、str_text2; /*构造函数初始化所有字段*/ 公共MyObject(上下文) {/*初始化…*/} /*线程从外部调用此方法以更新显示的值*/ 公共最终作废方法_A() { ((活动)getContext()).runonUIThread( 新的Runnable() { 公开募捐 { text1.setText(str_text1); text2.setText(str_text2); } }); } /*对象从外部调用此方法来初始化这两个值*/ 方法B(字符串str_text1,字符串str_text2) { this.str_text1=str_text1; this.str_text2=str_text2; } }
查看您正在使用最后一个setText()方法用text2重写text1的示例您是否在UI线程中设置文本?无法理解您的问题..您的意思是像计时器..然后您可以使用计时器来实现此功能