简体   繁体   English

Android的三个按钮状态(已按下,未按下和默认)

[英]Android three button states (pressed, not pressed and default)

When the layout loads, all buttons would have default state/bg (without state in selector). 加载布局时,所有按钮将具有默认状态/ bg(选择器中没有状态)。 When the button is pressed, the background for that button would change to button_pressed="true" and for all the others too button_pressed="false". 当按下按钮时,该按钮的背景将更改为button_pressed =“ true”,所有其他按钮的背景也会更改为button_pressed =“ false”。 Is it possible? 可能吗?

With this code a default background is button_pressed="false" item. 使用此代码,默认背景为button_pressed =“ false”项目。

<?xml version="1.0" encoding="UTF-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- pressed -->
<item
  android:state_pressed="true"
  android:drawable="@drawable/buttonClicked" />
<item 
android:state_pressed="false"
android:drawable="@drawable/buttonNotClicked" />
<item 
android:drawable="@drawable/button" />
</selector>

Your button_selector should look like below : 您的button_selector应该如下所示:

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

Here you can have 3 states ie button_normal (when button is in normal condition), button_focused (when button is focused to be pressed) and button_pressed (after the button has been pressed).. 在这里,您可以具有3种状态,即button_normal (当按钮处于正常状态时), button_focused (当按钮被聚焦以按下时)和button_pressed (在按下按钮之后)。

So use 4 different drawables(in /res/drawable folder) with names below : 因此,使用以下名称的4个不同的drawables(在/res/drawable文件夹中):

  1. button_selector.xml (For selector) button_selector.xml (用于选择器)
  2. button_normal.xml (normal button) button_normal.xml (普通按钮)
  3. button_focused.xml (button when focused) button_focused.xml (聚焦时的按钮)
  4. button_pressed.xml (button when pressed) button_pressed.xml (按下按钮时)

Hope it helps! 希望能帮助到你! Let me know if I can help you for anything else... 让我知道我是否可以帮助您...

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

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