要完成单选框显示,我们需要使用到RadioGroup和RadioButton(单选框),
RadioGroup用于对单选框进行分组,相同组内的单选框只有一个单选框能被选中。(例子代码请见下方备注栏)
RadioGroup.check(R.id.dotNet);将id名为dotNet的单选框设置成选中状态。
(RadioButton) findViewById(radioGroup.getCheckedRadioButtonId());//获取被选中的单选框。
调用setOnCheckedChangeListener()方法,处理单选框被选择事件,把RadioGroup.OnCheckedChangeListener实例作为参数传入
界面设计
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <RadioGroup android:id="@+id/radioGroup" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"> <RadioButton android:id="@+id/java" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="java" /> <RadioButton android:id="@+id/dotNet" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="dotNet" /> <RadioButton android:id="@+id/php" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="PHP" /> </RadioGroup> </LinearLayout>处理程序:
public void onCreate(Bundle savedInstanceState) { ...... RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup); radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { public void onCheckedChanged(RadioGroup group, int checkedId) { RadioButton radioButton = (RadioButton) findViewById(checkedId); Log.i(TAG, String.valueOf(radioButton.getText())); } }); }
private RadioButton homeBtn,catagoryBtn,carBtn,moreBtn,buyBtn; homeBtn = (RadioButton)findViewById(R.id.main_tab_home); catagoryBtn = (RadioButton)findViewById(R.id.main_tab_catagory); carBtn = (RadioButton)findViewById(R.id.main_tab_car); buyBtn = (RadioButton)findViewById(R.id.main_tab_buy); moreBtn = (RadioButton)findViewById(R.id.main_tab_more); OnClickListener handle = new OnClickListener(){ public void onClick(View v){ switch(v.getId()){ case R.id.main_tab_home: tabHost.setCurrentTabByTag("home"); break; case R.id.main_tab_catagory: tabHost.setCurrentTabByTag("catagory"); break; case R.id.main_tab_car: tabHost.setCurrentTabByTag("car"); break; case R.id.main_tab_buy: tabHost.setCurrentTabByTag("buy"); break; case R.id.main_tab_more: tabHost.setCurrentTabByTag("more"); break; } } }; homeBtn.setOnClickListener(handle); catagoryBtn.setOnClickListener(handle); carBtn.setOnClickListener(handle); buyBtn.setOnClickListener(handle); moreBtn.setOnClickListener(handle);
<RadioGroup android:id="@+id/main_tab_group" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:gravity="bottom" android:background="@drawable/main_navigation_background" android:orientation="horizontal" android:paddingTop="2.0dip"> <RadioButton android:id="@+id/main_tab_home" android:checked="true" style="@style/main_tab_bottom" android:drawableTop="@drawable/main_navigation_home" android:text="@string/main_navigation_home"/> <RadioButton android:id="@+id/main_tab_catagory" style="@style/main_tab_bottom" android:drawableTop="@drawable/main_navigation_catagory" android:text="@string/main_navigation_catagory"/> <RadioButton android:id="@+id/main_tab_car" style="@style/main_tab_bottom" android:drawableTop="@drawable/main_navigation_car" android:text="@string/main_navigation_car"/> <RadioButton android:id="@+id/main_tab_buy" style="@style/main_tab_bottom" android:drawableTop="@drawable/main_navigation_personal" android:text="@string/main_code_buy"/> <RadioButton android:id="@+id/main_tab_more" style="@style/main_tab_bottom" android:drawableTop="@drawable/main_navigation_more" android:text="@string/main_navigation_more"/> </RadioGroup>