gpt4 book ai didi

java - 在切换按钮的 RadioGroup 之间切换

转载 作者:行者123 更新时间:2023-11-30 02:36:16 25 4
gpt4 key购买 nike

所以,我的按钮排列如下:

这是我想要实现的目标的图像:

enter image description here

在图像中,第一行只是一行按钮,没有按下任何按钮。在第二行,只有第一个按钮被按下。在第三行,我点击了 4,只有第四个按钮被按下(不再是第一个)。

    <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="4"
android:rowCount="1"
android:orientation="horizontal"
>

<ToggleButton

android:id="@+id/number_zero"
android:layout_width="34sp"
android:layout_height="40sp"
android:textOn="@string/number_zero"
android:textOff="@string/number_zero"
android:onClick="onToggle"
android:checked="true"
android:background="@drawable/psc_number_color"
android:layout_margin="5sp"

/>
<ToggleButton
android:id="@+id/number_one"
android:layout_width="34sp"
android:layout_height="40sp"
android:textOn="@string/number_one"
android:textOff="@string/number_one"
android:onClick="onToggle"
android:checked="true"
android:background="@drawable/psc_number_color"
android:layout_margin="5sp"

/>

<ToggleButton
android:id="@+id/number_two"
android:layout_width="34sp"
android:layout_height="40sp"
android:textOn="@string/number_two"
android:textOff="@string/number_two"
android:background="@drawable/psc_number_color"
android:layout_margin="5sp"
/>
</RadioGroup>

我想知道是否可以在这五个按钮之间切换,如果我按下一个按钮,该按钮将保持按下状态。如果我单击该组中的另一个按钮,我之前单击的按钮将按下,而我单击的新按钮将被按下。

尝试在 pageradapter 中执行此操作:

   public void onToggle(View view) {
((RadioGroup)view.getParent()).check(view.getId());
// app specific stuff ..
}

public Object instantiateItem(ViewGroup parent, int position) {

one = (ToggleButton) view.findViewById(R.id.number_one);
two = (ToggleButton) view.findViewById(R.id.number_two);
three = (ToggleButton) view.findViewById(R.id.number_three);
four = (ToggleButton) view.findViewById(R.id.number_four);

final RadioGroup.OnCheckedChangeListener ToggleListener = new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(final RadioGroup radioGroup, final int i) {
for (int j = 0; j < radioGroup.getChildCount(); j++) {
final ToggleButton view = (ToggleButton) radioGroup.getChildAt(j);
view.setChecked(view.getId() == i);
}
}
};

one.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
one.toggle();
}
});

((RadioGroup) view.findViewById(R.id.toggleGroup)).setOnCheckedChangeListener(ToggleListener);

}

最佳答案

试试这个:我用 Checkbox [自定义复选框] 做了同样的事情。

enter image description here

@Override
public void onClick(View view) {

CheckBox cb = (CheckBox) view;


if(cb.isChecked()){

LinearLayout llLayout = (LinearLayout) cb.getParent();

for(int i=0; i<((ViewGroup)llLayout).getChildCount(); ++i) {
View nextChild = ((ViewGroup)llLayout).getChildAt(i);
if(nextChild instanceof CheckBox && nextChild.getId()==cb.getId() ){

}else if (nextChild instanceof CheckBox && nextChild.getId()!=cb.getId() ){

CheckBox cb2=(CheckBox) nextChild;
cb2.setChecked(false);
}
}

} else{
LinearLayout llLayout = (LinearLayout) cb.getParent();

for(int i=0; i<((ViewGroup)llLayout).getChildCount(); ++i) {
View nextChild = ((ViewGroup)llLayout).getChildAt(i);
if(nextChild instanceof CheckBox && nextChild.getId()==cb.getId() ){
CheckBox cb2=(CheckBox) nextChild;
cb2.setChecked(false);
}else if (nextChild instanceof CheckBox && nextChild.getId()!=cb.getId() ){
CheckBox cb2=(CheckBox) nextChild;
cb2.setChecked(false);

}
}
}
}

我的 XML:

<LinearLayout
android:id="@+id/llShift"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="4"
android:gravity="right"
android:orientation="horizontal" >

<CheckBox
android:id="@+id/cbMorning"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/custom_radio_button_morning"
android:paddingBottom="5dp"
android:paddingRight="3dp"
android:paddingTop="5dp" />

<CheckBox
android:id="@+id/cbEvning"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/custom_radio_button_evening"
android:paddingBottom="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp" />
</LinearLayout>

custom_radio_button_morning.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item
android:drawable="@drawable/morning_chk"
android:state_checked="true"/>
<item
android:drawable="@drawable/morning_unchk"
android:state_checked="false"/>
</selector>

enter image description here

enter image description here

关于java - 在切换按钮的 RadioGroup 之间切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26478328/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com