java 如何更改 Android 按钮颜色 onClick?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37316969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 02:23:59  来源:igfitidea点击:

How to change Android button color onClick?

javaandroidandroid-layoutbuttononclick

提问by user1871869

I wanted to change the android button color every time I click on a button. Once a user clicks a button, I want it so that the color changes. Then, when the button is pressed again, the color reverts back to what it was before. Here is my attempt:

每次单击按钮时,我都想更改 android 按钮的颜色。一旦用户单击一个按钮,我希望它改变颜色。然后,当再次按下按钮时,颜色会恢复到之前的颜色。这是我的尝试:

private void setupFollowButton(Button button, final Boolean isClicked) {
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Boolean isClickedDummy = !isClicked;
            if(isClickedDummy) {
                v.setBackgroundColor(Color.parseColor("#FF0000"));
            } else {
                v.setBackgroundColor(Color.parseColor("#FFFFFF"));
            }
        }
    });
}

Originally I wanted it so that isClicked = !isClickedso that I would know for certain that the isClickedvariable has changed and I can change the color. However, the method I have above only changes the isClickedto false and I can't seem to change it back to true. Is there any way I can figure this out? Any help would be appreciated. Thanks!

最初我想要它,isClicked = !isClicked这样我就可以肯定地知道isClicked变量已经改变并且我可以改变颜色。但是,我上面的方法仅将 更改isClicked为 false,而我似乎无法将其更改回true. 有什么办法可以解决这个问题吗?任何帮助,将不胜感激。谢谢!

回答by Ahmad Aghazadeh

You can xml drawable :

你可以 xml drawable :

<Button
     android:id="@+id/button1"
     android:background="@drawable/selector_xml_name"
     android:layout_width="200dp"
     android:layout_height="126dp"
     android:text="Hello" />

selector_xml_name.xml

selector_xml_name.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/numpad_button_bg_selected" android:state_selected="true"></item>
    <item android:drawable="@drawable/numpad_button_bg_pressed" android:state_pressed="true"></item>
    <item android:drawable="@drawable/numpad_button_bg_normal"></item>

</selector

回答by Vyacheslav

Do not forget to remove BackgroundResourceof your button if you want to change the background color.

BackgroundResource如果您想更改背景颜色,请不要忘记删除您的按钮。

That is, use:

也就是说,使用:

btn.setBackgroundResource(0);

btn.setBackgroundResource(0);

After that, 'usual button layout' will disappear and I will show the changes of setBackgroundmethod.

之后,“通常的按钮布局”将消失,我将展示setBackground方法的变化。

回答by Hello World

try this:

试试这个:

 isClicked = false;   

     private void setupFollowButton(Button button, final Boolean isClicked) {
            button.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    if(isClicked) {
                        v.setBackgroundColor(Color.parseColor("#FF0000"));
                        isClicked = false;
                    } else {
                        v.setBackgroundColor(Color.parseColor("#FFFFFF"));
                        isClicked = true;
                    }
                }
            });
        }

回答by michoprogrammer

You have to change the value of "isClickedDummy" and you have to use it as global.

您必须更改“isClickedDummy”的值,并且必须将其用作全局值。

Boolean isClickedDummy; // global after the declaration of your class

isClickedDummy = true; // in your onCreate() 

private void setupFollowButton(Button button, final Boolean isClicked) {
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            if(isClickedDummy) {
                v.setBackgroundColor(Color.parseColor("#FF0000"));
                isClickedDummy = false;
            } else {
                v.setBackgroundColor(Color.parseColor("#FFFFFF"));
                isClickedDummy = true;
            }
        }
    });
}