Android:更改切换按钮背景颜色,指示选项启用或禁用选项
我有一个切换按钮,按下时我想更改其背景。我的目标是即使在新闻事件发生后也将按钮的颜色更改为新颜色。当再次按下时,它会进入初始状态。
我使用了以下代码,但在我的情况下,“按下”事件后颜色不会保留。当按下按钮时,颜色会发生变化。但是,一旦按下事件结束,它就会恢复为按钮的原始颜色。
布局:
<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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论