由于使用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完成了线程布局,并且具有互斥选择的逻辑。