Description and use of a RadioButton RadioGroup
There are options in the identity of the selected projects, the need to use RadioGroup and RadioButton, to be recorded here, and we exchange.
Man of few words said, look at the code together
XML code
<RadioGroup
android:id="@+id/login_radiogroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal">
<RadioButton
android:id="@+id/admin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="@string/admin"/>
<RadioButton
android:id="@+id/tech"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="@string/tech"/>
<RadioButton
android:id="@+id/market"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="@string/market"/>
<RadioButton
android:id="@+id/guest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="@string/guest"/>
</RadioGroup>
This uses layout_gravity let RadioGroup centered, using the orientation property, horizontal so that the interior of the RadioButton placed horizontally, vertical is the vertical display.
JAVA code
Since in this instance Fragment layout which, with all the view.findViewById (), is removed in the view, then you can in an Activity.
private RadioGroup mRadioGroup;
mRadioGroup=(RadioGroup)view.findViewById(R.id.login_radiogroup);
Then listen for RadioGroup achieve at the event.
1 mRadioGroup.setOnCheckedChangeListener(new CheckListener()); 2 3 class CheckListener implements RadioGroup.OnCheckedChangeListener{ 4 5 @Override 6 public void onCheckedChanged(RadioGroup group, int checkedId) { 7 switch (checkedId){ 8 case R.id.admin: 9 //执行具体操作 10 break; 11 12 case R.id.tech: 13 //执行具体操作 14 break; 15 16 case R.id.market: 17 //执行具体操作 18 break; 19 20 case R.id.guest: 21 //执行具体操作 22 break; 23 24 default: 25 break; 26 27 } 28 } 29 }
是个菜鸟,有错误还希望大家能指出来。
欢迎大家有好的想法一起交流。