繁体   English   中英

setText 函数不会改变 TextView

[英]setText function does not change the TextView

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

我在 setText 方法中尝试了 String.valueof(int) 和 Integer.toString(int)

爪哇

 @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 代码

这可能与在主线程上运行所有内容有关。 你永远不应该在主线程上调用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();

如果你想弥补 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();

在尝试解决这个问题之前,让我们先了解它为什么会发生

原因

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

让我们试着运行这个块,你会注意到直到这个 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);
                    }
                });
            }
        }
    });
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM