【问题标题】:how can i make TextView change Text at Runtime?如何让 TextView 在运行时更改文本?
【发布时间】:2012-02-03 08:33:12
【问题描述】:

目前我的来源是这样的

    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 秒调用一次的方法的一部分。 TextViews 放置在 LinearLayout 上。

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

视图似乎没有重绘。

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

【问题讨论】:

  • 查看您正在使用最后一个 setText() 方法用 text2 重写 text1 的示例
  • 你在 UI Thread 中设置 Text 吗?
  • 无法理解你的问题..你的意思是像一个计时器..那么你可以使用计时器来实现这个

标签: android textview repaint


【解决方案1】:

也许您的错误取决于“text1”和“text2”的错误对象类型。 这个变量是字符串对象还是整数?如果你使用 Integer (int) 你需要 将变量值转换为字符串:

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

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

【讨论】:

    【解决方案2】:

    设置文本不应要求您使 TextView 无效来更改它。

    确定要这样做吗?

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        你可以使用处理程序来更新 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]);
                        }
        
                    }
        

        希望你能得到它......

        【讨论】:

          【解决方案5】:

          真正的类看起来更复杂,但这个例子展示了我的(工作)答案,应该清楚它是如何工作的。

          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;
              }
          }
          

          【讨论】:

            猜你喜欢
            • 2012-05-28
            • 2017-05-28
            • 2011-06-05
            • 1970-01-01
            • 2019-06-03
            • 2011-01-19
            • 1970-01-01
            • 2015-10-10
            相关资源
            最近更新 更多