【问题标题】:Android RadioGroup not workingAndroid RadioGroup 不工作
【发布时间】:2017-02-27 13:27:50
【问题描述】:

我正在尝试创建 2 行单选按钮,如下图所示。 但是当我直接放在上面时,我的广播组不起作用 线性布局。

我是安卓开发新手。所以如果有更好的选择。 欢迎他们

我的代码

<RadioGroup
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayout2"
        android:background="@android:color/holo_blue_dark">
        <RadioButton
            android:text="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/radioButton33" />
        <RadioButton
            android:text="2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/radioButton34" />
        <RadioButton
            android:text="3"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/radioButton35" />
        <RadioButton
            android:text="4"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/radioButton36" />
        <RadioButton
            android:text="5"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/radioButton37" />
        <RadioButton
            android:text="6"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/radioButton38" />
        <RadioButton
            android:text="7"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/radioButton39"
            android:layout_marginRight="1.0dp" />
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayout3"
        android:background="@android:color/holo_blue_dark">
        <RadioButton
            android:text="8"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/radioButton40" />
        <RadioButton
            android:text="9"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/radioButton41" />
        <RadioButton
            android:text="10"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/radioButton42" />
        <RadioButton
            android:text="11"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/radioButton43" />
        <RadioButton
            android:text="12"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/radioButton44" />
        <RadioButton
            android:text="13"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/radioButton45" />
        <RadioButton
            android:text="14"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/radioButton46" />
    </LinearLayout>
</RadioGroup>

谢谢

【问题讨论】:

标签: android xml xamarin


【解决方案1】:

您可以创建 2 个 Radio 组并处理 RadioButtonCliked 事件中的所有内容 就像那个例子

public void RadioButtonClicked(View view) {
    boolean checked = ((RadioButton) view).isChecked();

    switch(view.getId()) {
        case R.id.radioButton33: {  
            NameRadioGroup2.clearCheck();
            NameRadioGroup1.check(view.getId());
            break;
        }

        case R.id.radioButton40: {
            NameRadioGroup1.clearCheck();
            NameRadioGroup2.check(view.getId());          
            break;
        }
        {here implement cases for other radio buttons}
}



public void RadioButtonClicked(View view) {
    boolean checked = ((RadioButton) view).isChecked();
    if(ButtonFromRadioGroupOne(view.getId())
    {
       NameRadioGroup2.clearCheck();
       NameRadioGroup1.check(view.getId());
    }
    else
    {
       NameRadioGroup1.clearCheck();
       NameRadioGroup2.check(view.getId());
    }
}

private bool ButtonFromRadioGroupOne(int id)
{
    return id == R.id.radioButton33 || id == R.id.radioButton34 || id == R.id.radioButton35 || id == R.id.radioButton36 || id == R.id.radioButton37 || id == R.id.radioButton38 || id == R.id.radioButton39;
}

或者您可以为每个单选组设置 2 个 RadioButtonClicked 事件。

public void RadioButtonClicked1(View view) {
    RadioButtonClicked(NameRadioGroup1, NameRadioGroup2, view);
}

public void RadioButtonClicked2(View view) {
    RadioButtonClicked(NameRadioGroup2, NameRadioGroup1, view);
}

public void RadioButtonClicked(RadioGroup selected, RadioGroup cleared, View view) {
    boolean checked = ((RadioButton) view).isChecked();
    cleared.clearCheck();
    selected.check(view.getId());
}     

【讨论】:

  • 这会给我一个很大的转变。难道没有更好的方法吗?
  • 我添加了另一个没有开关的解决方案
【解决方案2】:

我修好了

首先我创建了一个扩展方法来从视图中获取孩子

static class Extention
    {
        public static List<T> Childs<T>(this ViewGroup view) where T : View
        {
            List<T> childs = new List<T>();
            for (int i = 0; i < view.ChildCount; i++)
            {
                var selectedView = view.GetChildAt(i) as T;
                if (view != null)
                {
                    childs.Add(selectedView);
                }
            }
            return childs;
        }
    }

我在 main 中添加了这段代码

LinearLayout Layout2 = FindViewById<LinearLayout>(Resource.Id.linearLayout2);
            LinearLayout Layout3 = FindViewById<LinearLayout>(Resource.Id.linearLayout3);
            Buttons = Layout2.Childs<RadioButton>();  
            Buttons.AddRange(Layout3.Childs<RadioButton>());
            foreach(RadioButton button in Buttons)
            {
                button.CheckedChange += RadioButton_Check;
            }

这是我的活动

 public bool InUse = false;
        private void RadioButton_Check(object sender, CompoundButton.CheckedChangeEventArgs e)
        {
            if (InUse) return;
            InUse = true;
            RadioButton Senderbutton = sender as RadioButton;
            foreach (RadioButton button in Buttons)
            {
                button.Checked = false;
            }
            Senderbutton.Checked = true;
            InUse = false;
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 2012-06-25
    • 2018-06-05
    • 1970-01-01
    • 2019-01-29
    相关资源
    最近更新 更多