RadioButton的隶属关系
public class RadioButton extends CompoundButton
java.lang.Object
android.view.View
android.widget.TextView
android.widget.Button
android.widget.CompoundButton
android.widget.RadioButton
![](http://hi.csdn.net/attachment/201112/14/0_13238558537mAg.gif)
RadioButton 就是我们常常讲的 “单选按钮”它只存在两种状态即 选择 和 未选 两种状态,用户的操作方式也就是点击一下它,与复选按钮相反,用户一旦选中就必须选择其中的一个,多个单选按钮通常与RadioGroup同时使用。当一个单选按钮组包含几个按钮的时候,选中其中一个的同时取消其他单选按钮,也就是上面我说到的,一旦选择后就必须是有一个选项是选中的,如果是单独使用的话 一旦选择则无法取消选择!(上图中是选中的RadioButton,此时是选中的男,而如果选择女的话,男就会取消选中!)
下面将我的例子代码贴到下面:
=main.xml代码====
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="男"
/>
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="女"
/>
</RadioGroup>
</LinearLayout>
==string.xml代码===
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">性别选择</string>
<string name="app_name">组件案例</string>
</resources>
下面的例子是当我们选中某个单选按钮后程序给我们返回的信息案例:
开始例子之前先看下常用监听事件:
下面是各种常用控件的事件监听的使用EditText(编辑框)的事件监听---OnKeyListenerRadioGroup、RadioButton(单选按钮)的事件监听---OnCheckedChangeListenerCheckBox(多选按钮)的事件监听---OnCheckedChangeListenerSpinner(下拉列表)的事件监听---OnItemSelectedListenerMenu(菜单)的事件处理---onMenuItemSelectedDialog(对话框)的事件监听---DialogInterface.OnClickListener()=======![]()
==== ViewDemoActivity ========package cn.view.demo; import android.app.Activity; import android.os.Bundle; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.Toast; public class ViewDemoActivity extends Activity { private RadioGroup group; private RadioButton nan,nv; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); group = (RadioGroup) this.findViewById(R.id.radio_gro); nan = (RadioButton) this.findViewById(R.id.radio_nan); nv = (RadioButton) this.findViewById(R.id.radio_nv); group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { public void onCheckedChanged(RadioGroup group, int checkedId) { if(checkedId ==nan.getId()){ showmsg("你好先生,设置成功"+nan.getText()); }else if(checkedId==nv.getId()){ showmsg("你好女士,设置成功"+nan.getText()); } } }); } private void showmsg(String str) { Toast.makeText(this, str, Toast.LENGTH_SHORT).show(); } } =======main.xml========
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<RadioGroup
android:id="@+id/radio_gro"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<RadioButton
android:id="@+id/radio_nan"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="男"
/>
<RadioButton
android:id="@+id/radio_nv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="女"
/>
</RadioGroup>
</LinearLayout>
这就是 RaidoButton 相关的学习!
其实上面的方法还可以使用 接口俩实现,只要 Activity 实现OnCheckedChangeListener 接口就可以在单独的方法中(onCheckedChanged() )实现上面的操作!
power by 白璐.中国