Android组件(widget)Raidobutton
2011-12-14 17:45
375 查看
RadioButton的隶属关系
public class RadioButton extends CompoundButton
java.lang.Object
android.view.View
android.widget.TextView
android.widget.Button
android.widget.CompoundButton
android.widget.RadioButton
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(编辑框)的事件监听---OnKeyListener
RadioGroup、RadioButton(单选按钮)的事件监听---OnCheckedChangeListener
CheckBox(多选按钮)的事件监听---OnCheckedChangeListener
Spinner(下拉列表)的事件监听---OnItemSelectedListener
Menu(菜单)的事件处理---onMenuItemSelected
Dialog(对话框)的事件监听---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 白璐.中国
相关文章推荐
- Android 基本组件 Button和Tab_Widget
- No resource found that matches the given name 'android:TextAppearance.Material.Widget.Button.Inverse
- Android自定义Button组件
- android--widget介绍之Button
- Android Widget桌面组件教程整理(一)
- <style name="Base.Widget.AppCompat.ActionButton" parent="android:Widget.Material.Acti
- No resource found that matches the given name 'android:TextAppearance.Material.Widget.Button...
- 【J】Android-Widget桌面小组件
- No resource found that matches the given name 'android:Widget.Material.ActionButton'
- android用户界面-组件Widget-选项卡Tab
- java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.wid
- Android视图组件: Button
- Android 桌面组件【widget】初探
- 导入android-support-v7-appcompat出现问题: android:Widget.Material.ActionButton
- Android 解决No resource found that matches android:TextAppearance.Material.Widget.Button.Inverse问题
- 导入android-support-v7-appcompat出现问题: android:Widget.Material.ActionButton
- 关于java.lang.ClassCastException: android.widget.Button cannot be cast to android.widget.Edit 错误的解决
- Android中No resource found that matches android:TextAppearance.Material.Widget.Button.Inverse问题
- Android 桌面组件【widget】初探
- java.lang.NoSuchMethodError: android.widget.Button.setBackground