简体   繁体   English

Android单击时更改背景颜色

[英]Android Change background color when clicked

I'm trying to change the background color of a Button when it is clicked, I can change the background color when I have the Button pressed down when I release the Button the button returns to its default color. 我试图更改单击Button时的背景颜色,我可以在松开Button时按下Button时更改背景颜色,使按钮返回其默认颜色。 I'm not sure where to go from here I have been trying to find a solution to this problem. 我不确定从哪里去,我一直在努力寻找解决这个问题的方法。

` `

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    tools:context="com.example.game.dice.MainActivity">

    <Button
        android:id="@+id/but1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 1"
        android:background="@drawable/button_color"
        />
    <Button
        android:id="@+id/but2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 2"
        android:background="@drawable/button_color"/>
</LinearLayout>

` `

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
    android:drawable="@color/button_pressed"/>
<item android:state_focused="true"
    android:drawable="@color/button_focused"/> 
<item android:drawable="@color/button_default"/> 
</selector>

Here is a quick setup in Java on how to do it; 这是Java中的快速设置方法。

private Button mButton;
mButton = (Button) findViewById(R.id.but1);

mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mButton.setBackgroundColor(Color.BLACK);
        }
    });

You basically make a ClickListener than reacts whenever the button is clicked. 基本上,您使ClickListener比单击按钮时反应。 From here we change the buttons background color to Black. 在这里,我们将按钮的背景色更改为黑色。 If you want it to change with each click, you can apply more logic. 如果希望每次单击更改它,则可以应用更多逻辑。

In your drawable, the last <item> indicates the default state. 在您的可绘制对象中,最后一个<item>表示默认状态。 Change that. 改变它。

Edit: 编辑:

I'm sorry i undestood wrong. 对不起,我没错。 You have to do this in Java or Kotlin whichever you are using. 无论使用哪种方式,都必须使用Java或Kotlin进行。 This is more business logic than it is "view". 这是比“视图”更多的业务逻辑。

Try like this: 尝试这样:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:color="@color/button_pressed" />
<item android:state_focused="true" android:state_pressed="true" android:color="@color/button_pressed" />
<item android:state_focused="false" android:state_pressed="true" android:color="@color/button_default" />
<item android:color="@color/button_pressed" />

Try this one 试试这个


<selector xmlns:android="http://schemas.android.com/apk/res/android">

<!-- Non focused states -->
<item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/on_the_go"
    android:color="@color/colorText" />
<item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/on_the_go_selected"
    android:color="@color/colorSkyBlue"/>

<!-- Focused states -->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/on_the_go"
    android:color="@color/colorText"/>
<item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/on_the_go_selected"
    android:color="@color/colorSkyBlue"/>

<!-- Pressed -->
<item android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/on_the_go"
    android:color="@color/colorText"/>
<item android:state_pressed="true" android:drawable="@drawable/on_the_go_selected"
    android:color="@color/colorSkyBlue" />

</selector>

Happy to help you 很高兴为您服务

Did you try this ? 你尝试过这个吗?

myButton.setBackgroundColor(Color.RED);

This code is in your onClick method. 这段代码在您的onClick方法中。

try this 尝试这个

button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                button.setBackgroundColor(ContextCompat.getColor(getActivity(),R.color.colorBlack));
            }
        });

NOTE if you wan't to reset a button's background color to default than use below code 注意:如果您不想将按钮的背景色重置为默认值,请使用以下代码

button.setBackgroundResource(android.R.drawable.btn_default);

这是因为默认情况下,当触摸按钮时,它将单击而不是聚焦。如果要使按钮聚焦并在按下时更改其颜色,请将其添加到xml中的按钮中。

android:focusableInTouchMode="true"

The correct xml is : 正确的xml是:

`<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@color/button_pressed"/>
<item android:state_focused="false"
android:drawable="@color/button_focused"/> 
<item android:drawable="@color/button_default"/> 
</selector>`

you can't focused all true. 你不能全神贯注。

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

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