简体   繁体   English

如何更改单击的按钮的颜色,但使其只能同时更改一个按钮的颜色?

[英]How can I change the color of a button that is clicked but make it so that only one buttons' color can be changed at the same time?

How can I make it so that instead of displaying text whenever a button is clicked, instead the background color of each button changes and only one button can be that color at the same time?我怎样才能做到这一点,而不是在单击按钮时显示文本,而是每个按钮的背景颜色发生变化,并且同时只有一个按钮可以是该颜色?

package com.example.scrolltes1;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.KeyEventDispatcher;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import static android.graphics.Color.BLUE;
import static android.graphics.Color.GRAY;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    boolean constrain = true;

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


        Button button1 = findViewById(R.id.button1);
        Button button2 = findViewById(R.id.button2);
        Button button3 = findViewById(R.id.button3);

        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
            switch (v.getId()) {
                case R.id.button1:
                    Toast.makeText(this, "Button 1 clicked", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.button2:
                    Toast.makeText(this, "Button 2 clicked", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.button3:
                    Toast.makeText(this, "Button 3 clicked", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    }

would it just be possible to replace everything inside of the Toast.makeText parenthesis with something that changes background color?是否可以用改变背景颜色的东西替换 Toast.makeText 括号内的所有内容?

you could create a custom layout with a specific color for each button:您可以为每个按钮创建具有特定颜色的自定义布局:

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.button1
        Toast ToastMessage = Toast.makeText(this,"Button 1 clicked", Toast.LENGTH_SHORT);
        View toastView = ToastMessage.getView();
        toastView.setBackgroundResource(R.layout.custom_color);
        ToastMessage.show();
        break;

    // more cases
    }
   }

See this article for more details about the solution: https://www.android-examples.com/change-toast-message-background-color-in-android/有关解决方案的更多详细信息,请参阅本文: https://www.android-examples.com/change-toast-message-background-color-in-android/

If this helps, don't hesitate to accept it as an answer:)如果这有帮助,请不要犹豫接受它作为答案:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM