将“stop button”改为“start button”

abithluo  于 2021-07-14  发布在  Java
关注(0)|答案(1)|浏览(275)

下午好。我开始学习androidstudio,并决定创建一个应用程序“trafficlight”。它应该每2秒改变一次颜色,当颜色改变时,开始按钮应该在停止按钮上改变。这就是问题所在。我看了视频,写了类似的代码,但什么也没发生:(。如果对您来说不难,请检查我的代码并给我建议如何改进:)。这是我的密码:
主要活动的xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
    android:background="#FFFFFF"
    tools:context=".MainActivity" >

    <LinearLayout
        android:id="@+id/Balb1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginTop="108dp"
        android:background="@color/Grey"
        android:orientation="horizontal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

    </LinearLayout>

    <LinearLayout
        android:id="@+id/Balb2"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginTop="16dp"
        android:background="#ACA5A5"
        android:orientation="horizontal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/Balb3"></LinearLayout>

    <LinearLayout
        android:id="@+id/Balb3"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginTop="16dp"
        android:background="#ACA5A5"
        android:orientation="horizontal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/Balb1">

    </LinearLayout>

    <Button
        android:id="@+id/startButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:text="Start"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/Balb2" />

</androidx.constraintlayout.widget.ConstraintLayout>

代码如下:

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity implements onClick {

    private LinearLayout B1, B2, B3;
    private Button button1;
    private boolean startOrStop = false;

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

        B1 = findViewById(R.id.Balb1);
        B2 = findViewById(R.id.Balb2);
        B3 = findViewById(R.id.Balb3);
        button1 = findViewById(R.id.startButton);
    }

    public void onClickStart(View view){

        if(!startOrStop) {
            button1.setText(getString(R.string.stop));
            startOrStop = true;

            new Thread((Runnable) () -> {
                while (startOrStop){
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e){
                        e.printStackTrace();
                    }
                }
            }).start();
        }

        else {
            startOrStop = false;
            button1.setText(getString(R.string.start));
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        startOrStop = false;
    }
}
dgiusagp

dgiusagp1#

问题出现在以下代码中:

new Thread((Runnable) () -> {
            while (startOrStop){
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e){
                    e.printStackTrace();
                }
            }
        }).start();

while是无限运行的,因为在循环中没有使startorstop变量为false的逻辑。它还会阻塞ui线程。
所以应该编写一些逻辑,使startorstop变量在循环中为false。否则你不需要任何while循环,这里没有什么简单的东西,写了下面的代码就可以了。

new Thread((Runnable) () -> {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e){
                    e.printStackTrace();
                }
        }).start();

相关问题