Android:在RadioGroup中實現RadioButton的線性布局

來源:互聯網
上載者:User

 

 
由於使用RadioGroup,無奈只能實現單排,單列的選項按鈕效果。經過查閱文檔發現RadioGroup繼承LinearLayout,就想著使用嵌套布局來實現,於是就有了如下想法:

[html]
 <RadioGroup >  
      <LinearLayout>  
          <RadioButto />  
          <RadioButto />  
      </LinearLayout>  
      <LinearLayou>  
          <RadioButto />  
          <RadioButto />  
      </LinearLayout>  
      <LinearLayout >  
            <RadioButto />  
      </LinearLayout>  
</RadioGroup>  

 <RadioGroup >
      <LinearLayout>
          <RadioButto />
          <RadioButto />
      </LinearLayout>
      <LinearLayou>
          <RadioButto />
          <RadioButto />
      </LinearLayout>
      <LinearLayout >
            <RadioButto />
      </LinearLayout>
</RadioGroup> 但是運行後才發現,RadioButton間,並沒有選項按鈕相互斥選擇的效果了。後來查詢各種資料和思考,發現一種替代的解決辦法,可能稍有麻煩,但卻是能實現需求,現在展示如下:

布局檔案main.xml,通過多組的RadioGroup來實現RadioButton的線性布局:

[html]
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:orientation="vertical" >  
    <RadioGroup  
        android:id="@+id/orderBy1"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:orientation="horizontal" >  
        <RadioButton  
            android:id="@+id/orderBy1.1"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="名稱" />  
        <RadioButton  
            android:id="@+id/orderBy1.2"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="日期" />  
    </RadioGroup>  
    <RadioGroup  
        android:id="@+id/orderBy2"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:orientation="horizontal" >  
        <RadioButton  
            android:id="@+id/orderBy2.1"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="類型" />  
        <RadioButton  
            android:id="@+id/orderBy2.2"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="大小" />  
    </RadioGroup>  
    <RadioGroup  
        android:id="@+id/orderBy3"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:orientation="horizontal" >  
        <RadioButton  
            android:id="@+id/orderBy3.3"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="系統預設" />  
    </RadioGroup>  
</LinearLayout>  

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <RadioGroup
        android:id="@+id/orderBy1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
        <RadioButton
            android:id="@+id/orderBy1.1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="名稱" />
        <RadioButton
            android:id="@+id/orderBy1.2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="日期" />
    </RadioGroup>
    <RadioGroup
        android:id="@+id/orderBy2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
        <RadioButton
            android:id="@+id/orderBy2.1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="類型" />
        <RadioButton
            android:id="@+id/orderBy2.2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="大小" />
    </RadioGroup>
    <RadioGroup
        android:id="@+id/orderBy3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
        <RadioButton
            android:id="@+id/orderBy3.3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="系統預設" />
    </RadioGroup>
</LinearLayout> 主檔案MainActivity.java,通過使用RadioGruop.OnCheckedChangeListener來處理不同組RadioGroup的互斥邏輯:

[java]
public class MainActivity extends Activity {  
  
    private RadioGroup radioGroup1;  
    private RadioGroup radioGroup2;  
    private RadioGroup radioGroup3;  
    private Boolean changeedGroup = false;  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
  
        radioGroup1 = (RadioGroup) findViewById(R.id.orderBy1);  
        radioGroup1.setOnCheckedChangeListener(new MyRadioGroupOnCheckedChangedListener());  
        radioGroup2 = (RadioGroup) findViewById(R.id.orderBy2);  
        radioGroup2.setOnCheckedChangeListener(new MyRadioGroupOnCheckedChangedListener());  
        radioGroup3 = (RadioGroup) findViewById(R.id.orderBy3);  
        radioGroup3.setOnCheckedChangeListener(new MyRadioGroupOnCheckedChangedListener());  
    }  
  
    class MyRadioGroupOnCheckedChangedListener implements OnCheckedChangeListener {  
        @Override  
        public void onCheckedChanged(RadioGroup group, int checkedId) {  
            if (!changeedGroup) {  
                changeedGroup = true;  
                if (group == radioGroup1) {  
                    radioGroup2.clearCheck();  
                    radioGroup3.clearCheck();  
                } else if (group == radioGroup2) {  
                    radioGroup1.clearCheck();  
                    radioGroup3.clearCheck();  
                } else if (group == radioGroup3) {  
                    radioGroup1.clearCheck();  
                    radioGroup2.clearCheck();  
                }  
                changeedGroup = false;  
            }  
       }  
    }  
}  

public class MainActivity extends Activity {
 
    private RadioGroup radioGroup1;
    private RadioGroup radioGroup2;
    private RadioGroup radioGroup3;
    private Boolean changeedGroup = false;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        radioGroup1 = (RadioGroup) findViewById(R.id.orderBy1);
        radioGroup1.setOnCheckedChangeListener(new MyRadioGroupOnCheckedChangedListener());
        radioGroup2 = (RadioGroup) findViewById(R.id.orderBy2);
        radioGroup2.setOnCheckedChangeListener(new MyRadioGroupOnCheckedChangedListener());
        radioGroup3 = (RadioGroup) findViewById(R.id.orderBy3);
        radioGroup3.setOnCheckedChangeListener(new MyRadioGroupOnCheckedChangedListener());
    }
 
    class MyRadioGroupOnCheckedChangedListener implements OnCheckedChangeListener {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if (!changeedGroup) {
                changeedGroup = true;
                if (group == radioGroup1) {
                    radioGroup2.clearCheck();
                    radioGroup3.clearCheck();
                } else if (group == radioGroup2) {
                    radioGroup1.clearCheck();
                    radioGroup3.clearCheck();
                } else if (group == radioGroup3) {
                    radioGroup1.clearCheck();
                    radioGroup2.clearCheck();
                }
                changeedGroup = false;
            }
       }
    }
} 運行後,3行2列的RadioButton完成了線程布局,並且具有互斥選擇的邏輯。

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.