Android Studio设置背景颜色按钮没有做任何事情

问题描述 投票:0回答:1

我正在开发一个Android应用程序,其中有一些按钮。我们的想法是创建一个带有上方问题按钮和按钮可能答案的测试。

我尝试从不是UI线程的线程着色这些按钮,并且着色无效但程序不会给出任何错误。通过在UI线程上运行也是如此。

public void discoverAnswer(final String rep){
        this.sleeping = true;
        Log.d(TAG, "discoverReponse: step1");
        btn1.setBackgroundColor(btn1.getText().toString().equals(rep) ? Color.GREEN:Color.RED);
        btn2.setBackgroundColor(btn2.getText().toString().equals(rep) ? Color.GREEN:Color.RED);
        btn3.setBackgroundColor(btn3.getText().toString().equals(rep) ? Color.GREEN:Color.RED);
        btn4.setBackgroundColor(btn4.getText().toString().equals(rep) ? Color.GREEN:Color.RED);
        Log.d(TAG, "discoverReponse step2");
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Log.d(TAG, "discoverReponse: step3");
                btn1.setBackgroundColor(btn1.getText().toString().equals(rep) ? Color.GREEN:Color.RED);
                btn2.setBackgroundColor(btn2.getText().toString().equals(rep) ? Color.GREEN:Color.RED);
                btn3.setBackgroundColor(btn3.getText().toString().equals(rep) ? Color.GREEN:Color.RED);
                btn4.setBackgroundColor(btn4.getText().toString().equals(rep) ? Color.GREEN:Color.RED);
                Log.d(TAG, "discoverReponse step4");     
            }
        });
    }

目前我得到了所有出现的输出(步骤1-4),但按钮没有变成红色或绿色。

android android-button ui-thread
1个回答
0
投票

好的,一步一步来。有了这个布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="aaaa" />

    <Button
        android:id="@+id/button_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="bbbb" />

    <Button
        android:id="@+id/button_3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="cccc" />

    <Button
        android:id="@+id/button_4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="dddd" />

</LinearLayout>

看起来像那样:

before click

您可以创建简单的逻辑:

package training.com.myapplication;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    // All of your buttons
    Button btn1;
    Button btn2;
    Button btn3;
    Button btn4;

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

        // "Find" buttons in layout
        btn1 = findViewById(R.id.button_1);
        btn2 = findViewById(R.id.button_2);
        btn3 = findViewById(R.id.button_3);
        btn4 = findViewById(R.id.button_4);

        // your correct response
        String response = "aaaa";

        // Adding listeners to all of buttons
        btn1.setOnClickListener(v -> {
            discoverAnswer(response);
        });

        btn2.setOnClickListener(v -> {
            discoverAnswer(response);
        });

        btn3.setOnClickListener(v -> {
            discoverAnswer(response);
        });

        btn4.setOnClickListener(v -> {
            discoverAnswer(response);
        });
    }

    public void discoverAnswer(final String rep) {
        // Change color all of the buttons
        runOnUiThread(() -> {
            btn1.setBackgroundColor(btn1.getText().toString().equals(rep) ? Color.GREEN : Color.RED);
            btn2.setBackgroundColor(btn2.getText().toString().equals(rep) ? Color.GREEN : Color.RED);
            btn3.setBackgroundColor(btn3.getText().toString().equals(rep) ? Color.GREEN : Color.RED);
            btn4.setBackgroundColor(btn4.getText().toString().equals(rep) ? Color.GREEN : Color.RED);
        });
    }
}

或者使用Buttons列表:

package training.com.myapplication;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    // List of buttons
    List<Button> listOfButtons = new ArrayList<>();

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

        listOfButtons.addAll(Arrays.asList(
                findViewById(R.id.button_1),
                findViewById(R.id.button_2),
                findViewById(R.id.button_3),
                findViewById(R.id.button_4)
        ));

        // your correct response
        String response = "aaaa";

        // Adding listeners to all of buttons - for API < 24
        for (Button button : listOfButtons) {
            button.setOnClickListener(v -> discoverAnswer(response));
        }
    }

    public void discoverAnswer(final String rep) {
        // Change color all of the buttons
        runOnUiThread(() -> {
            for (Button button : listOfButtons) {
                button.setBackgroundColor(button.getText().toString().equals(rep) ? Color.GREEN : Color.RED);
            }
        });
    }
}

点击按钮后,您可以预期:

  • 正确的 - 将是绿色的
  • 其余的 - 红色

after click

© www.soinside.com 2019 - 2024. All rights reserved.