Android三个按钮状态(按下,未按下并默认)

时间:2014-12-24 11:06:40

标签: android button background state

当布局加载时,所有按钮都将具有默认状态/ bg(在选择器中没有状态)。按下按钮时,该按钮的背景将变为button_pressed =“true”,而对于所有其他按钮,也会变为button_pressed =“false”。有可能吗?

使用此代码,默认背景为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>

1 个答案:

答案 0 :(得分:1)

您的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>

在这里你可以有3个状态,即button_normal(当按钮处于正常状态时),button_focused(按下按钮时被按下)和button_pressed(按钮被按下后)按下)..

因此,使用4个不同的drawable(在/res/drawable文件夹中),名称如下:

  1. button_selector.xml(适用于选择器)
  2. button_normal.xml(正常按钮)
  3. button_focused.xml(关注时按钮)
  4. button_pressed.xml(按下按钮)
  5. 希望它有所帮助! 如果我可以帮助你做任何事情,请告诉我......