代码之家  ›  专栏  ›  技术社区  ›  Digant Jani

如何在同一布局的不同列表视图中取消选中单选按钮。?

  •  2
  • Digant Jani  · 技术社区  · 11 年前

    我已经解决了许多关于单选按钮的问题,但这是我多年以来的想法。我在不同的列表视图中有5个单选按钮,请告诉我如何在访问其他单选按钮时取消选中任何单选按钮,并获取所选单选按钮的值。 以下是代码。

    自定义目录.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:ignore="HardcodedText,RtlHardcoded,NestedWeights,UselessParent" >
    
        <LinearLayout
            android:layout_width="350dp"
            android:layout_height="400dp"
            android:background="#000000"
            android:orientation="vertical"
            android:weightSum="8" >
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_weight="1"
                android:background="#E3EBED"
                android:orientation="horizontal" >
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_marginLeft="10dp"
                    android:text="Screen timeout"
                    android:textColor="#449BA4"
                    android:textSize="21sp" />
            </LinearLayout>
    
            <RadioGroup
                android:id="@+id/rg_all"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_weight="1"
                android:background="#F5F5F5"
                android:orientation="horizontal"
                android:weightSum="10" >
    
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_marginLeft="10dp"
                    android:layout_weight="9"
                    android:text="15 seconds"
                    android:textColor="#000000"
                    android:textSize="18sp"
                    tools:ignore="HardcodedText" />
    
                <RadioButton
                    android:id="@+id/rb_15sec"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_weight="1"
                    />
            </LinearLayout>
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_weight="1"
                android:background="#F5F5F5"
                android:orientation="horizontal"
                android:weightSum="10" >
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_marginLeft="10dp"
                    android:layout_weight="9"
                    android:text="30 seconds"
                    android:textColor="#000000"
                    android:textSize="18sp"
                    tools:ignore="HardcodedText" />
    
                <RadioButton
                    android:id="@+id/rb_30sec"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_weight="1" />
            </LinearLayout>
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_weight="1"
                android:background="#F5F5F5"
                android:orientation="horizontal"
                android:weightSum="10" >
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_marginLeft="10dp"
                    android:layout_weight="9"
                    android:text="1 minute"
                    android:textColor="#000000"
                    android:textSize="18sp" />
    
                <RadioButton
                    android:id="@+id/rb_1min"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_weight="1" />
            </LinearLayout>
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_weight="1"
                android:background="#F5F5F5"
                android:orientation="horizontal"
                android:weightSum="10" >
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_marginLeft="10dp"
                    android:layout_weight="9"
                    android:text="2 minutes"
                    android:textColor="#000000"
                    android:textSize="18sp" />
    
                <RadioButton
                    android:id="@+id/rb_2min"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_weight="1" />
            </LinearLayout>
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_weight="1"
                android:background="#F5F5F5"
                android:orientation="horizontal"
                android:weightSum="10" >
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_marginLeft="10dp"
                    android:layout_weight="9"
                    android:text="5 minutes"
                    android:textColor="#000000"
                    android:textSize="18sp" />
    
                <RadioButton
                    android:id="@+id/rb_5min"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_weight="1"
                    android:checked="true" />
            </LinearLayout>
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_weight="1"
                android:background="#F5F5F5"
                android:orientation="horizontal"
                android:weightSum="10" >
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_marginLeft="10dp"
                    android:layout_weight="9"
                    android:text="10 minutes"
                    android:textColor="#000000"
                    android:textSize="18sp" />
    
                <RadioButton
                    android:id="@+id/rb_10min"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_weight="1" />
            </LinearLayout>
    </RadioGroup>
            <LinearLayout
                android:id="@+id/ll_cancel"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_weight="1"
                android:background="#C9DCE0"
                android:gravity="center"
                android:orientation="horizontal" >
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="Cancel"
                    android:textColor="#000000"
                    android:textSize="18sp" />
            </LinearLayout>
        </LinearLayout>
    
    </RelativeLayout>
    

    自定义对话框.java

    package com.example.coustomdialogue;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.RadioButton;
    import android.widget.RadioGroup;
    import android.widget.RadioGroup.OnCheckedChangeListener;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class CustomDialogue extends Activity {
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.custom_dialogue);
    
            final RadioGroup rg = (RadioGroup) findViewById(R.id.rg_all);
    
    
        final RadioButton rb1 = (RadioButton)findViewById(R.id.rb_10min);
        final RadioButton rb2 = (RadioButton)findViewById(R.id.rb_15sec);
        final RadioButton rb3 = (RadioButton)findViewById(R.id.rb_1min);
        final RadioButton rb4 = (RadioButton)findViewById(R.id.rb_2min);
        final RadioButton rb5 = (RadioButton)findViewById(R.id.rb_30sec);
        final RadioButton rb6 = (RadioButton)findViewById(R.id.rb_5min);
    
    
        rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    
            @Override
    
            public void onCheckedChanged(RadioGroup group, int checkedId) {
    
            if(checkedId == R.id.rb_10min){
                rg.clearCheck();
                rb2.setChecked(false);
                rb3.setChecked(false);
                rb4.setChecked(false);
                rb5.setChecked(false);
                rb6.setChecked(false);
            }else if(checkedId == R.id.rb_15sec){
                rg.clearCheck();
                rb1.setChecked(false);
                rb3.setChecked(false);
                rb4.setChecked(false);
                rb5.setChecked(false);
                rb6.setChecked(false);
            }else if(checkedId == R.id.rb_1min){
                rg.clearCheck();
                rb2.setChecked(false);
                rb4.setChecked(false);
                rb1.setChecked(false);
                rb5.setChecked(false);
                rb6.setChecked(false);
                }else if(checkedId == R.id.rb_2min){
                    rg.clearCheck();
                    rb2.setChecked(false);
                    rb3.setChecked(false);
                    rb5.setChecked(false);
                    rb1.setChecked(false);
                    rb6.setChecked(false);
                    }else if(checkedId == R.id.rb_30sec){
                        rg.clearCheck();
                        rb2.setChecked(false);
                        rb3.setChecked(false);
                        rb4.setChecked(false);
                        rb6.setChecked(false);
                        rb1.setChecked(false);
                        }else{
                            rg.clearCheck();
                            rb2.setChecked(false);
                            rb2.setChecked(false);
                            rb3.setChecked(false);
                            rb4.setChecked(false);
                            rb5.setChecked(false);
                            rb1.setChecked(false);
                        }
    
            }
    
            });
    
    }
    }
    
    1 回复  |  直到 11 年前
        1
  •  0
  •   Digant Jani    11 年前

    我得到了以下问题的真正答案。我在每个单选按钮上都使用了Click事件,从而解决了问题。每当我点击单选按钮时,我会将选中更改为取消选中(如果有)。无需使用RadioGroup。 希望这对你们都有帮助。