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

huangapple 未分类评论45阅读模式
英文:

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?

问题

如何使得在点击按钮时,不显示文本,而是更改每个按钮的背景颜色,且同一时间只有一个按钮可以具有该颜色?

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:
                button1.setBackgroundColor(Color.RED);  // Change background color
                button2.setBackgroundColor(GRAY);      // Reset other button colors
                button3.setBackgroundColor(GRAY);
                break;
            case R.id.button2:
                button1.setBackgroundColor(GRAY);
                button2.setBackgroundColor(Color.GREEN);
                button3.setBackgroundColor(GRAY);
                break;
            case R.id.button3:
                button1.setBackgroundColor(GRAY);
                button2.setBackgroundColor(GRAY);
                button3.setBackgroundColor(Color.BLUE);
                break;
        }
    }
}

是否只需将Toast.makeText括号内的所有内容替换为更改背景颜色的内容?

英文:

<h1>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? </h1>

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, &quot;Button 1 clicked&quot;, Toast.LENGTH_SHORT).show();
                    break;
                case R.id.button2:
                    Toast.makeText(this, &quot;Button 2 clicked&quot;, Toast.LENGTH_SHORT).show();
                    break;
                case R.id.button3:
                    Toast.makeText(this, &quot;Button 3 clicked&quot;, Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    }

<p1>would it just be possible to replace everything inside of the Toast.makeText parenthesis with something that changes background color?</p1>

答案1

得分: 0

你可以为每个按钮创建一个特定颜色的自定义布局:

@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;

        // 更多情况
    }
}

查看这篇文章获取更多关于解决方案的详细信息:https://www.android-examples.com/change-toast-message-background-color-in-android/

如果这对你有帮助,请毫不犹豫地接受它作为答案 如何在按钮被点击时更改其颜色,但同时只能更改一个按钮的颜色?

英文:

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,&quot;Button 1 clicked&quot;, 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/

If this helps, don't hesitate to accept it as an answer 如何在按钮被点击时更改其颜色,但同时只能更改一个按钮的颜色?

huangapple
  • 本文由 发表于 2020年7月26日 07:44:09
  • 转载请务必保留本文链接:https://java.coder-hub.com/63094693.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定