【问题标题】:Toggle between a RadioGroup of Toggle Buttons在切换按钮的 RadioGroup 之间切换
【发布时间】:2014-10-21 03:18:25
【问题描述】:

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

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

在图像中,第一行只是一排没有按下按钮的按钮。 在第二行中,只有第一个按钮被按下。在第三行,我点击了 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);

}

【问题讨论】:

  • 您可以使用 RadioGroup 并自定义单选按钮,使它们看起来像简单的按钮
  • 如何使无线电组水平而不是垂直?
  • 使用android:orientation='horizontal/vertical'RadioGroup.setOrientation(LinearLayout.HORIZONTAL);
  • 如果我使用单选按钮,如何去掉中间的圆圈?我可以使用 RadioGroup 然后 ToggleButton 吗?如果是这样,我如何获得单选按钮效果?

标签: java android button toggle


【解决方案1】:

试试这个:我用 Checkbox [ custom checkbox ] 做了同样的事情。

@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>

【讨论】:

  • Nvm。我修好了它。我有个问题。我应该把java代码放在哪里?现在,我把它绑在一个按钮上。这应该适用于我认为的所有按钮还是..?也就是说,监听器与什么相关联?
  • 我有一个问题。到目前为止,如果我取下我的可绘制对象,那么当我在它们之间进行选择时,切换按钮就像单选按钮一样。问题是,当我将可绘制对象添加到按钮时(例如 android:background="@drawable/psc_number_color")。这些按钮不再像他们应该的那样工作。我不知道为什么。
  • 我可以看看你的drawable吗?
  • 我将您的答案标记为最佳答案,即使我仍有可绘制问题。如果您想查看,我提出了一个新问题:stackoverflow.com/questions/26554764/…
  • 查看我的 git 存储库。只为你。 github.com/iamsarker/android-apps/tree/master/…
【解决方案2】:

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

【讨论】:

  • 抱歉,我添加了一张图片。我想要的不是那些切换按钮。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-04
  • 1970-01-01
  • 2019-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多