Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android:在RadioGroup中實現RadioButton的線性布局

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

編輯:關於Android編程

 

 
由於使用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完成了線程布局,並且具有互斥選擇的邏輯。

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved