【问题标题】:setText function does not change the TextViewsetText 函数不会改变 TextView
【发布时间】:2019-05-25 23:25:23
【问题描述】:

应用程序以我想要的方式运行我可以在 Logcat 中看到 但文本视图没有改变并保持默认值 我也尝试以编程方式更改按钮启用状态,但保持不变,没有任何改变!

我在 setText 方法中都试过了 String.valueof(int) 和 整数.toString(int)

java

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         play =(Button) findViewById(R.id.button4);
         Pause =(Button) findViewById(R.id.button5);
        hourstext =(TextView) findViewById(R.id.textView1);
        mintext =(TextView) findViewById(R.id.textView2);
        sectext =(TextView) findViewById(R.id.textView3);


    }

    void playb(View v)  {


        while (!ispause) {
            sec = 0 ;
            while (sec < 60) {
                SystemClock.sleep(1000);
                sec++;
               sectext.setText(Integer.toString(sec));
                Log.d("this", "sec value=" + sec);
            }
            sec = 0;
            min++;
            Log.d("this","min value ="+min);

           mintext.setText(String.valueOf(min));

        }

    }

XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView3"
        android:layout_width="114dp"
        android:layout_height="94dp"
        android:layout_marginTop="159dp"
        android:layout_marginEnd="16dp"
        android:layout_x="274dp"
        android:layout_y="120dp"
        android:gravity="center|center_horizontal"
        android:text="00"
        android:textSize="40sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/pauseButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="72dp"
        android:layout_marginTop="116dp"
        android:layout_marginEnd="93dp"
        android:layout_x="217dp"
        android:layout_y="296dp"
        android:enabled="false"
        android:onClick="playb"
        android:text="Pause"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/playbutton"
        app:layout_constraintTop_toBottomOf="@+id/textView3" />

    <Button
        android:id="@+id/playbutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="70dp"
        android:layout_marginTop="116dp"
        android:layout_x="63dp"
        android:layout_y="293dp"
        android:onClick="playb"
        android:text="playb"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView2" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="120dp"
        android:layout_height="97dp"
        android:layout_marginTop="156dp"
        android:layout_marginEnd="17dp"
        android:layout_x="139dp"
        android:layout_y="117dp"
        android:gravity="center|center_horizontal"
        android:text="00"
        android:textSize="40sp"
        app:layout_constraintEnd_toStartOf="@+id/textView3"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="103dp"
        android:layout_height="94dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="159dp"
        android:layout_x="11dp"
        android:layout_y="117dp"
        android:gravity="center_horizontal|center_vertical"
        android:text="00"
        android:textSize="40sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

我没有收到错误消息!该应用程序继续工作,但没有更新的 textView

我已经包含了我的 XML 代码

【问题讨论】:

  • 尝试用 settext(sec+"") 代替 settext(Integer.toString(sec))
  • 我也试过同样的结果
  • 请添加xml,我认为textView可能会改变但它没有显示在布局中
  • 如果您通过断点跟踪代码,可能会出现问题
  • 我已经包含了 XML

标签: java android


【解决方案1】:

这可能与在主线程上运行所有内容有关。你不应该在主线程上调用sleep,否则你会阻塞 UI。

单击按钮时,您应该在后台线程上启动计数器。然后你需要更新主线程上的TextView。

使用 RxJava 可以很容易地实现:

private Disposable disposable;

disposable = Observable.interval(1, 1, TimeUnit.SECONDS)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(value -> {
            // Update UI
        });

停止计数器:

disposable.dispose();

【讨论】:

    【解决方案2】:

    如果你想让计数器保持 60 秒,你可以使用这个代码

    long time=60;
            new CountDownTimer(time*1000, 1000)
             { 
        public void onTick(long millisUntilFinished) {
         mTextField.setText("seconds : " + (time-millisUntilFinished /1000));
         } 
            public void onFinish() {
         mTextField.setText("done!"); 
        } 
            }.start();
    

    【讨论】:

      【解决方案3】:

      在尝试解决此问题之前,请先了解它发生的原因

      原因

      当您调用 View.setText() 时,Android 实际上并没有立即设置文本。它会将所有这些设置文本作品推送到主线程上的队列中,以便稍后在有空闲时间时执行。

      让我们尝试运行这个块,你会注意到直到这个 while 循环完成,View.setText() 才会完成。

      void playb(View v) {
          min = 0
          while (min < 1000000) {
              min++
              Log.d("this", "min value =$min")
      
              mintext.setText(String.valueOf(min))
          }
      }
      

      所以在你的情况下,实际上 TextView 仍然会被设置,但是在 while 循环结束之前你不会看到变化。

      解决方案

      您应该将此 while 循环移动到另一个线程,您可以简单地使用 AsyncTask 或 HandlerThread 来实现

      例如。使用 HandlerThread:

      void playb() {
      
          // Start a new background thread
          HandlerThread thread = new HandlerThread("");
          thread.start();
      
          // Obtain the handler of new background thread
          Handler handler = new Handler(thread.getLooper());
      
          // Obtain the handler of main thread (UI Thread)
          final Handler mainHandler = new Handler(this.getMainLooper());
      
          // Create a runnable and send it to background thread to execute
          handler.post(new Runnable() {
      
              public final void run() {
      
                  // Do the job
                  int min = 0;
                  while(true) {
                      int sec = 0;
                      while(sec < 60) {
                          SystemClock.sleep(1000L);
                          sec ++;
                          final int currentSec = sec;
      
                          // Send the update-text job to main thread to execute
                          mainHandler.post(new Runnable() {
                              public final void run() {
                                  secText.setText(currentSec);
                              }
                          });
                      }
                      sec = 0;
                      min++;
                      final int currentMin = min;
      
                      // Send the update-text job to main thread to execute
                      mainHandler.post(new Runnable() {
                          public final void run() {
                              minText.setText(currentMin);
                          }
                      });
                  }
              }
          });
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多