【发布时间】:2014-10-25 12:41:08
【问题描述】:
现在,我有一个单选组作为父级,切换按钮作为子级。
所以是这样的:
布局.xml
<RadioGroup
android:id="@+id/toggleGroup"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:useDefaultMargins="true"
android:layout_column="0"
android:columnCount="11"
android:rowCount="1"
android:orientation="horizontal"
>
<ToggleButton
android:id="@+id/number_zero"
android:layout_width="34sp"
android:layout_height="40sp"
android:layout_row="0"
android:layout_column="0"
android:textOn="@string/number_zero"
android:textOff="@string/number_zero"
android:layout_margin="5sp"
/>
<ToggleButton
android:id="@+id/number_one"
android:layout_width="34sp"
android:layout_height="40sp"
android:layout_row="0"
android:layout_column="1"
android:textOn="@string/number_one"
android:textOff="@string/number_one"
android:layout_margin="5sp"
/>
</RadioGroup>
</GridLayout>
Adapter中的代码sn-p:
ToggleButton one;
one = (ToggleButton) view.findViewById(R.id.number_one);
one.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
buttonValue = 1;
ToggleButton tb = (ToggleButton) view;
if(tb.isChecked()){
RadioGroup radioGroup = (RadioGroup) tb.getParent();
for(int i=0; i<(radioGroup).getChildCount(); ++i) {
View nextChild = (radioGroup).getChildAt(i);
if(nextChild instanceof ToggleButton && nextChild.getId()==tb.getId() ){
}else if (nextChild instanceof ToggleButton && nextChild.getId()!=tb.getId() ){
ToggleButton tb2=(ToggleButton) nextChild;
tb2.setChecked(false);
}
}
} else{
RadioGroup radioGroup = (RadioGroup) tb.getParent();
for(int i=0; i<(radioGroup).getChildCount(); ++i) {
View nextChild = (radioGroup).getChildAt(i);
if(nextChild instanceof ToggleButton && nextChild.getId()==tb.getId() ){
ToggleButton tb2=(ToggleButton) nextChild;
tb2.setChecked(false);
}else if (nextChild instanceof ToggleButton && nextChild.getId()!=tb.getId() ){
ToggleButton tb2=(ToggleButton) nextChild;
tb2.setChecked(false);
}
}
}
}
});
以上内容针对我拥有的每个按钮重复。在此示例中为按钮 0 和 1。
可绘制:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:state_checked="true" >
<shape>
<solid
android:color="@color/button_selected" />
<stroke
android:width="1dp"
android:color="@color/button_selected" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item >
<item>
<shape android:state_checked="false">
<solid
android:color="@color/number_button_grey" />
<stroke
android:width="1dp"
android:color="@color/number_button_grey" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
</selector>
我想要的行为:
我想在按钮之间切换,以便一次只按下一个按钮。例如,在这种情况下,如果我单击按钮零,则将按下零并且不按下按钮一。如果我单击按钮一,则按钮一被按下,而按钮零则不被按下。我希望按下状态为真。
出于某种原因,如果我删除了上面按钮的可绘制对象,我可以像单选按钮行为一样轻松切换,当我按下按钮时,它会保持按下状态。但是,当我应用我的可绘制对象时,按钮在按下时不会保持按下状态。只有当我单击按钮并用手指按住按钮时,状态才会改变。如果我移开手指,它会返回按下状态 = false。
我不确定为什么使用可绘制对象,这不是我想要的行为。
编辑:这是一个关于当我将切换按钮更改为单选按钮以及当我拥有切换按钮时的行为的视频。我在单选按钮之间切换,红色选择没有保持不变。我对切换按钮做同样的事情。 https://www.dropbox.com/s/9sinptgoucv1hvn/VIDEO0043.mp4?dl=0
【问题讨论】:
-
为什么在你的第二个项目中,形状有 android:state_checked 属性?
-
只是为了强制在未选择按钮时显示灰色。我应该删除它吗?
-
每个按钮都有一个默认状态。并且该项目不需要任何属性集。我不知道它是否会起作用,如果你删除它。但这肯定无济于事。看例子here
-
是的,改变它没有帮助。我仍然有同样的问题。有什么想法吗?
-
@DuZi,我在帖子中添加了一个短视频来查看我的应用程序的问题。
标签: java android button android-drawable