【发布时间】:2014-06-24 10:16:09
【问题描述】:
我有一组 3 个按钮,希望在单击特定按钮时更改其颜色和文本,并在单击另一个按钮时再次恢复正常。
我是第一次使用选择器,我不清楚状态,任何人都可以帮助我解决这个问题。
这就是我正在做的事情。
这是我的 3 个按钮
<LinearLayout
android:id="@+id/lineartab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center|center_horizontal"
android:orientation="horizontal"
android:weightSum="3" >
<Button
android:id="@+id/allMessagesBT"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/message_top_btn_selector"
android:singleLine="false"
android:text="All \nMessages"
android:textColor="#7b818b"
android:textSize="14sp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginBottom="3dp"
android:layout_marginTop="3dp"
android:background="@drawable/line" />
<Button
android:id="@+id/createNewBT"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/message_top_btn_selector"
android:singleLine="false"
android:text="Create \nNew"
android:textColor="#7b818b"
android:textSize="14sp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginBottom="3dp"
android:layout_marginTop="3dp"
android:background="@drawable/line" />
<Button
android:id="@+id/deleteBT"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/message_top_btn_selector"
android:singleLine="false"
android:text="Delete \nSelected"
android:textColor="#7b818b"
android:textSize="14sp" />
</LinearLayout>
这是我的选择器 xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/login_edittext" android:state_selected="true"></item>
<item android:drawable="@drawable/btn_accept_normal" android:state_pressed="true</item>
<item android:drawable="@drawable/btn_accept_hover"></item>
</selector>
我想做的是在单击按钮后更改背景颜色并保持这种状态直到单击另一个按钮
【问题讨论】:
-
发布实际代码以更改您尝试过的文本和颜色..
-
我认为你必须在代码中使用 setclickListener for Button
-
当我单击按钮时,它会暂时更改其背景,然后恢复正常,但我想保持背景更改,直到按下下一个按钮。我可以用选择器来做还是我需要用 JAVA 编码来做
-
有什么办法可以使用选择器,因为我需要在大多数地方实现这一点
-
您可以在您想要的所有类中创建一种处理此问题的方法,获取一组视图作为参数,并且必须更改女巫位置的一个索引,然后设置背景或您想要的任何内容视图
标签: android button selector onclicklistener