首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将"stop-button“改为"start-button”

将"stop-button“改为"start-button”
EN

Stack Overflow用户
提问于 2021-04-20 02:06:33
回答 1查看 26关注 0票数 0

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

Main_activity的xml:

代码语言:javascript
复制
<?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>

下面是代码:

代码语言:javascript
复制
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;
    }
}
EN

回答 1

Stack Overflow用户

发布于 2021-04-20 02:40:02

问题出现在以下代码中:

代码语言:javascript
复制
new Thread((Runnable) () -> {
            while (startOrStop){
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e){
                    e.printStackTrace();
                }
            }
        }).start();

您的while是无限运行的,因为在循环中没有逻辑来使startOrStop变量为假。它还会阻塞UI线程。

因此,您应该编写一些逻辑,以便在循环内将startOrStop变量设为false。否则,你在这里不需要任何while循环,它不是什么简单的东西,只需编写下面的代码就可以了。

代码语言:javascript
复制
new Thread((Runnable) () -> {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e){
                    e.printStackTrace();
                }
        }).start();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67167151

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档