【发布时间】:2011-01-21 06:02:22
【问题描述】:
我想要具有两种状态的图像按钮 i) 正常 ii) 触摸(或单击)。
我在 imagebutton 背景中设置了 normal 图片,我正在尝试更改 image(pressed)从 onclick 方法,但它不会改变。
我希望如果我按下图像按钮,那么图像应该从正常变为按下,直到我按下其他按钮但它不会发生。
谁能建议我如何使用选择器或在运行时做到这一点?
这是我的图像按钮面板代码。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:gravity="bottom"
android:id="@+id/buttonpanel">
<ImageButton android:id="@+id/buttonhome"
android:layout_width="80dp"
android:layout_height="36dp"
android:focusable="true"
android:background="@drawable/homeselector">
</ImageButton>
<ImageButton android:id="@+id/buttonsearch"
android:layout_height="36dp"
android:layout_width="80dp"
android:background="@drawable/searchselector"
android:focusable="true">
</ImageButton>>
<ImageButton android:id="@+id/buttonreg"
android:layout_height="36dp"
android:layout_width="80dp"
android:background="@drawable/registerselector"
android:focusable="true">
</ImageButton>>
<ImageButton android:id="@+id/buttonlogin"
android:layout_height="36dp"
android:layout_width="80dp"
android:background="@drawable/loginselector"
android:focusable="true">
</ImageButton>
</LinearLayout>
我的选择器 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="@drawable/homehover"
/> <item android:state_focused="true" android:drawable="@drawable/homehover"
/> <item android:drawable="@drawable/home" /> <item android:state_window_focused="true"
android:drawable="@drawable/homehover" /> -->
<item android:state_enabled="false" android:drawable="@drawable/homehover" />
<item android:state_pressed="true" android:state_enabled="true"
android:drawable="@drawable/homehover" />
<item android:state_focused="true" android:state_enabled="true"
android:drawable="@drawable/homehover" />
<item android:state_enabled="true" android:drawable="@drawable/home" />
</selector>
我也尝试在 ontouch 和 onclick 事件上更改图像资源,但没有帮助。
【问题讨论】:
-
我认为您正在寻找类似this 的东西。我以前问过类似的问题。