【问题标题】:How to Autoincrement the TextView value in Android Studio如何在 Android Studio 中自动增加 TextView 的值
【发布时间】:2021-02-11 03:54:26
【问题描述】:

我想在 Android Studio 中自动增加 TextView 的值,但我面临的问题是 > 当我运行以下代码时,它只在代码完成时显示应用程序界面。有什么解决办法吗?

package com.example.assignment2;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import static android.os.SystemClock.sleep;

public class MainActivity extends AppCompatActivity{
private TextView tv;
int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv = findViewById(R.id.t);
    while(count<10000000) {
    count++;
    tv.setText("" + count);
    }
}

}

【问题讨论】:

  • Android Studio 只是一个 IDE 环境。重大缺陷在于您的代码中,您的循环(同时)通过 10B 记录只是为同一文本列执行一组文本

标签: java android xml textview


【解决方案1】:
   new Thread(new Runnable(){
        @Override
        public void run() {
            int counter = 0;
            while (counter < 10000000) {
                counter++;
                final String counterStr = counter.toString();
                tv.post(new Runnable(){
                    @Override
                    public void run() {
                        tv.setText(counterStr);
                    }
                });
                Thread.sleep(1000);
                }
            }
        }).start();

【讨论】:

    【解决方案2】:

    这里的问题是:

    在 Android 上,有一个叫做 主线程 的东西。该线程负责渲染应用程序的所有 UI,如果该线程忙于任何其他任务(例如运行一段时间,循环数为 10.000.000),您的 UI 将冻结,因为没有可用于渲染 UI 的资源。为避免此类问题,您应该在单独的线程中运行大部分代码,并在主/UI 线程上运行所有 UI 更新(两者是同一件事)。进行这些更改,您的代码将如下所示:

    package com.example.assignment2;
    import androidx.appcompat.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.*;
    import static android.os.SystemClock.sleep;
    
    public class MainActivity extends AppCompatActivity{
    private TextView tv;
    int count = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = findViewById(R.id.t);
        //Creating a new thread
            new Thread(new Runnable() {
                @Override
                public void run() {
                    //This while will run on this new thread
                    while(count<10000000) {
                        count++;
                        /*To ensure that the TextView update will run on the right thread
                        * we should run this update inside the runOnUiThread method*/
                        MainActivity.this.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                tv.setText("" + count);
                            }
                        });
                    }
                }
            });
    }
    

    要了解有关线程的更多信息,请查看这篇文章:

    https://developer.android.com/topic/performance/threads

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多