您好,欢迎访问三七文档
当前位置:首页 > 医学/心理学 > 药学 > RadioButton浅析
一.RadioButton单选按钮RadioButton(单选按钮)在Android开发中应用的非常广泛,比如一些选择项的时候,会用到单选按钮。它是一种单个圆形单选框双状态的按钮,可以选择或不选择。在RadioButton没有被选中时,用户能够按下或点击来选中它。但是,与复选框相反,用户一旦选中就不能够取消选中。实现RadioButton由两部分组成,也就是RadioButton和RadioGroup配合使用.RadioGroup是单选组合框,可以容纳多个RadioButton的容器.在没有RadioGroup的情况下,RadioButton可以全部都选中;当多个RadioButton被RadioGroup包含的情况下,RadioButton只可以选择一个。并用setOnCheckedChangeListener来对单选按钮进行监听下面的具体的例子:MainActivity.java1.packagecom.android.radiobutton;2.3.importandroid.app.Activity;4.importandroid.os.Bundle;5.importandroid.widget.RadioGroup;6.importandroid.widget.Toast;7.8.publicclassMainActivityextendsActivity{9.10.//声明RadioGroup11.RadioGroupraGroup1,raGroup2;12.@Override13.publicvoidonCreate(BundlesavedInstanceState){14.super.onCreate(savedInstanceState);15.setContentView(R.layout.main);16.17.//通过findViewById获得RadioGroup对象18.raGroup1=(RadioGroup)findViewById(R.id.radioGroup1);19.//添加事件监听器20.raGroup1.setOnCheckedChangeListener(newRadioGroup.OnCheckedChangeListener(){21.22.@Override23.publicvoidonCheckedChanged(RadioGroupgroup,intcheckedId){24.//TODOAuto-generatedmethodstub25.if(checkedId==R.id.radioBtn1){26.Toast.makeText(MainActivity.this,你来自广东省,Toast.LENGTH_LONG).show();27.}28.elseif(checkedId==R.id.radioBtn2){29.Toast.makeText(MainActivity.this,你来自广西省,Toast.LENGTH_LONG).show();30.}31.else{32.Toast.makeText(MainActivity.this,你来自湖南省,Toast.LENGTH_LONG).show();33.}34.}35.});36.raGroup2=(RadioGroup)findViewById(R.id.radioGroup2);37.raGroup2.setOnCheckedChangeListener(newRadioGroup.OnCheckedChangeListener(){38.39.@Override40.publicvoidonCheckedChanged(RadioGroupgroup,intcheckedId){41.//TODOAuto-generatedmethodstub42.if(checkedId==R.id.radioBtn4){43.Toast.makeText(MainActivity.this,你的性别是男,Toast.LENGTH_LONG).show();44.}45.else{46.Toast.makeText(MainActivity.this,你的性别是女,Toast.LENGTH_LONG).show();47.}48.}49.});50.}51.}main.xml1.?xmlversion=1.0encoding=utf-8?2.LinearLayoutxmlns:android=:orientation=vertical4.android:layout_width=fill_parent5.android:layout_height=fill_parent6.7.TextView8.android:layout_width=fill_parent9.android:layout_height=wrap_content10.android:text=@string/hello111./12.RadioGroup13.android:id=@+id/radioGroup114.android:layout_width=wrap_content15.android:layout_height=wrap_content16.android:orientation=vertical17.18.RadioButton19.android:id=@+id/radioBtn120.android:layout_width=wrap_content21.android:layout_height=wrap_content22.android:text=@string/radioBtn123./24.RadioButton25.android:id=@+id/radioBtn226.android:layout_width=wrap_content27.android:layout_height=wrap_content28.android:text=@string/radioBtn229./30.RadioButton31.android:id=@+id/radioBtn332.android:layout_width=wrap_content33.android:layout_height=wrap_content34.android:text=@string/radioBtn335./36./RadioGroup37.!--在两个RadioGroup之间画条横线--38.View39.android:layout_width=match_parent40.android:layout_height=1dp41.android:background=#ffffff42./43.TextView44.android:layout_width=fill_parent45.android:layout_height=wrap_content46.android:text=@string/hello247./48.RadioGroup49.android:id=@+id/radioGroup250.android:layout_width=wrap_content51.android:layout_height=wrap_content52.android:orientation=vertical53.54.RadioButton55.android:id=@+id/radioBtn456.android:layout_width=wrap_content57.android:layout_height=wrap_content58.android:text=@string/radioBtn459.android:textColor=#ffffff60./61.RadioButton62.android:id=@+id/radioBtn563.android:layout_width=wrap_content64.android:layout_height=wrap_content65.android:text=@string/radioBtn566./67./RadioGroup68./LinearLayoutstrings.xml1.?xmlversion=1.0encoding=utf-8?2.resources3.stringname=hello1你来自哪个省/string4.stringname=hello2你的性别是/string5.stringname=app_name单选按钮测试/string6.stringname=radioBtn1广东/string7.stringname=radioBtn2广西/string8.stringname=radioBtn3湖南/string9.stringname=radioBtn4男/string10.stringname=radioBtn5女/string11./resources效果图:RadioButton的另一种效果:要实现上面的效果,只要在main.xml布局文件中的RadioButton/加入android:button=@nullandroid:drawableRight=@android:drawable/btn_radio即可,代码如下所示:1.?xmlversion=1.0encoding=utf-8?2.LinearLayoutxmlns:android=:orientation=vertical4.android:layout_width=fill_parent5.android:layout_height=fill_parent6.7.TextView8.android:layout_width=fill_parent9.android:layout_height=wrap_content10.android:text=@string/hello111./12.RadioGroup13.android:id=@+id/radioGroup114.android:layout_width=wrap_content15.android:layout_height=wrap_content16.android:orientation=vertical17.18.RadioButton19.android:id=@+id/radioBtn120.android:layout_width=wrap_content21.android:layout_height=wrap_content22.android:text=@string/radioBtn123.android:button=@null24.android:drawableRight=@android:drawable/btn_radio25./26.RadioButton27.android:id=@+id/radioBtn228.android:layout_width=wrap_content29.android:layout_height=wrap_content30.android:text=@string/radioBtn231.android:button=@null32.android:drawableRight=@android:drawable/btn_radio33./34.RadioButt
本文标题:RadioButton浅析
链接地址:https://www.777doc.com/doc-2848035 .html