从实施 Runnable 的 class 访问 TextView - Android Studio

Accessing to TextView from class implementing Runnable - Android Studio

我有一个 class ExampleRunnable,它在某些限制下调用该方法来更改值百分比,而 TextView 将在设置可见性一行后显示它。我正在尝试显示此值,直到我不会停止此线程。

我的应用程序冻结,因为我无法访问 TextView。是否有任何解决方案,可以访问对它们进行操作?

主要class:

public class ScooterDashboard extends AppCompatActivity implements LocationListener {

   
    TextView textViewBatteryPercentage;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scooter_dashboard);

        
        textViewBatteryPercentage = findViewById(R.id.textViewBatteryPercentage);

        batBluetooth = findViewById(R.id.startBatteryMeasurement);


        batBluetooth.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                stopThread(view);
                return true;
            }
        });

        batBluetooth.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.R)
            @Override
            public void onClick(View view) {
                if(stopThread){
                    startThread(view);
                    textViewBatteryDistance.setText(Double.toString(batteryDistance));
                    textViewBatteryPercentage.setText(Double.toString(batteryPercentage));
                }
            }
        });

        
    }
...
public void startThread(View view) {
        stopThread = false;
        ExampleRunnable runnable = new ExampleRunnable();
        new Thread(runnable).start();

    }

    public void stopThread(View view) {
        stopThread = true;
    }



    class ExampleRunnable extends ScooterDashboard implements Runnable {
        @Override
        public void run() {
            while (!stopThread){
                
               //do while/try/catches...
                
                    ...
                    if(currentBatteryVoltage > 0 && batterySettingsDefined){
                        setTextBattery();    <- PROBLEM 
                    }
                //more try/catches...
            }
        }
    }

    private void setTextBattery() {
        setBatteryPercentage();
        ...
    }


    private void setBatteryPercentage() {
        double percentage = ((currentBatteryVoltage - bottomCutOffX) * 100) * (upperCutOffX - bottomCutOffX);

       
textViewBatteryPercentage.setText(Double.toString(batteryPercentage)); <- problem here
        textViewBatteryPercentage.setVisibility(View.VISIBLE); <- problem here
        
        if(percentage > 90){
            batBluetooth.setImageResource(R.drawable.bat100);
        }
        ...

        batteryPercentage = percentage; //not important, its value in ScooterDasboard, i omitted many in this code example
    }

}

runOnUiThread(()->{


textViewBatteryPercentage.setText(Double.toString(batteryPercentage));
 textViewBatteryPercentage.setVisibility(View.VISIBLE); 
})

用上面的 code.Ui 组件替换您的设置文本无法在单独的 threads.UI 内部访问,只能在 UI 线程内部访问。如果您需要访问单独线程中的 UI 组件,请使用上面的代码。

如果您在片段中,请按以下方式使用

getActivity().runOnUiThread(()->{

//put your UI access code here

});