切换按钮的RadioGroup之间切换

时间:2014-10-21 03:18:25

标签: java android button toggle

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

这是我想要实现的目标的形象:

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);

}

2 个答案:

答案 0 :(得分:1)

试试这个:我使用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

答案 1 :(得分:0)

您应该使用自定义切换按钮 - 请检查此链接herehere