Android:更改切换按钮背景颜色,指示选项启用或禁用选项

发布于 2024-12-20 12:46:47 字数 2990 浏览 4 评论 0原文

我有一个切换按钮,按下时我想更改其背景。我的目标是即使在新闻事件发生后也将按钮的颜色更改为新颜色。当再次按下时,它会进入初始状态。

我使用了以下代码,但在我的情况下,“按下”事件后颜色不会保留。当按下按钮时,颜色会发生变化。但是,一旦按下事件结束,它就会恢复为按钮的原始颜色。

布局:

<ToggleButton android:id="@+id/price_activate_button" 
 android:layout_width="wrap_content" 
 android:textSize="15dp" 
 android:text="@string/string_price" 
 android:layout_alignParentLeft="true"
 android:layout_height="wrap_content" 
 android:background="@drawable/button_blue" 
 style="@style/ButtonText" 
 android:textOn="Price" 
 android:textOff="Price"></ToggleButton>

风格:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" 
  android:state_pressed="true">
<shape>
<solid
 android:color="#859dbc" />
<stroke
 android:width="1dp"
 android:color="#859dbc" />
<corners
 android:radius="2dp" />
<padding
 android:left="5dp"
 android:top="5dp"
 android:right="5dp"
 android:bottom="5dp" />
</shape>
</item>
<item android:state_pressed="true" >
<shape>
<solid
 android:color="#859dbc" />
<stroke
 android:width="1dp"
 android:color="#859dbc" />
<corners
 android:radius="2dp" />
<padding
 android:left="5dp"
 android:top="5dp"
 android:right="5dp"
 android:bottom="5dp" />
</shape>
</item>
<item android:state_focused="true"  android:state_pressed="false" >
<shape>
<solid
 android:color="#2c68e7" />
<stroke
 android:width="1dp"
 android:color="#2c68e7" />
<corners
 android:radius="2dp" />
<padding
 android:left="5dp"
 android:top="5dp"
 android:right="5dp"
 android:bottom="5dp" />
</shape>
</item>          
<item android:state_checked="true">
<shape>
<solid
 android:color="#2c68e7" />
<stroke
 android:width="1dp"
 android:color="#2c68e7" />
<corners
 android:radius="2dp" />
<padding
 android:left="5dp"
 android:top="5dp"
 android:right="5dp"
 android:bottom="5dp" />
</shape>
</item>
<item android:state_checked="false">
<shape>
<solid
 android:color="#859dbc" />
<stroke
 android:width="1dp"
 android:color="#859dbc" />
<corners
 android:radius="2dp" />
<padding
 android:left="5dp"
 android:top="5dp"
 android:right="5dp"
 android:bottom="5dp" />
</shape>
</item>   
</selector>

活动类别:

final ToggleButton price_button = (ToggleButton)findViewById(R.id.price_activate_button);

price_button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        if(price_button.isChecked()){
            Toast.makeText(getApplicationContext(), "You have chosen: Yes", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(getApplicationContext(), "You have chosen: No", Toast.LENGTH_LONG).show();
        }
    }
});

I have a toggle button, the back ground of which I would like to change when pressed. My objective is to change the color of the button even after the press event to a new color. When it is pressed again, it goes to initial state.

I have used the following code, but in my case the color is not retained after the event of 'Press'. When the button is pressed the color changes. But However it reverts back to the original color of the button as soon as the press event ends.

Layout:

<ToggleButton android:id="@+id/price_activate_button" 
 android:layout_width="wrap_content" 
 android:textSize="15dp" 
 android:text="@string/string_price" 
 android:layout_alignParentLeft="true"
 android:layout_height="wrap_content" 
 android:background="@drawable/button_blue" 
 style="@style/ButtonText" 
 android:textOn="Price" 
 android:textOff="Price"></ToggleButton>

Style:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" 
  android:state_pressed="true">
<shape>
<solid
 android:color="#859dbc" />
<stroke
 android:width="1dp"
 android:color="#859dbc" />
<corners
 android:radius="2dp" />
<padding
 android:left="5dp"
 android:top="5dp"
 android:right="5dp"
 android:bottom="5dp" />
</shape>
</item>
<item android:state_pressed="true" >
<shape>
<solid
 android:color="#859dbc" />
<stroke
 android:width="1dp"
 android:color="#859dbc" />
<corners
 android:radius="2dp" />
<padding
 android:left="5dp"
 android:top="5dp"
 android:right="5dp"
 android:bottom="5dp" />
</shape>
</item>
<item android:state_focused="true"  android:state_pressed="false" >
<shape>
<solid
 android:color="#2c68e7" />
<stroke
 android:width="1dp"
 android:color="#2c68e7" />
<corners
 android:radius="2dp" />
<padding
 android:left="5dp"
 android:top="5dp"
 android:right="5dp"
 android:bottom="5dp" />
</shape>
</item>          
<item android:state_checked="true">
<shape>
<solid
 android:color="#2c68e7" />
<stroke
 android:width="1dp"
 android:color="#2c68e7" />
<corners
 android:radius="2dp" />
<padding
 android:left="5dp"
 android:top="5dp"
 android:right="5dp"
 android:bottom="5dp" />
</shape>
</item>
<item android:state_checked="false">
<shape>
<solid
 android:color="#859dbc" />
<stroke
 android:width="1dp"
 android:color="#859dbc" />
<corners
 android:radius="2dp" />
<padding
 android:left="5dp"
 android:top="5dp"
 android:right="5dp"
 android:bottom="5dp" />
</shape>
</item>   
</selector>

Activity Class:

final ToggleButton price_button = (ToggleButton)findViewById(R.id.price_activate_button);

price_button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        if(price_button.isChecked()){
            Toast.makeText(getApplicationContext(), "You have chosen: Yes", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(getApplicationContext(), "You have chosen: No", Toast.LENGTH_LONG).show();
        }
    }
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文