Android UI控件系列RadioButton(1).docx

上传人:牧羊曲112 文档编号:3152442 上传时间:2023-03-11 格式:DOCX 页数:5 大小:37.72KB
返回 下载 相关 举报
Android UI控件系列RadioButton(1).docx_第1页
第1页 / 共5页
Android UI控件系列RadioButton(1).docx_第2页
第2页 / 共5页
Android UI控件系列RadioButton(1).docx_第3页
第3页 / 共5页
Android UI控件系列RadioButton(1).docx_第4页
第4页 / 共5页
Android UI控件系列RadioButton(1).docx_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
资源描述

《Android UI控件系列RadioButton(1).docx》由会员分享,可在线阅读,更多相关《Android UI控件系列RadioButton(1).docx(5页珍藏版)》请在三一办公上搜索。

1、Android UI控件系列RadioButton单选按钮RadioButton在Android平台上也应用的非常多,比如一些选择项的时候,会用到单选按钮,实现单选按钮由两部分组成,也就是RadioButton和RadioGroup配合使用。 RadioButton的单选按钮; RadioGroup是单选组合框,用于将RadioButton框起来; 在没有RadioGroup的情况下,RadioButton可以全部都选中; 当多个RadioButton被RadioGroup包含的情况下,RadioButton只可以选择一个; 注意:单选按钮的事件监听用setOnCheckedChangeLis

2、tener来对单选按钮进行监听 例子: 一道选择题,选择哪个城市美女最多,当然,这个就是为了测试 RadioTest.java packageorg.loulijun.radio; importandroid.app.Activity; importandroid.os.Bundle; importandroid.view.Gravity; importandroid.widget.RadioButton; importandroid.widget.RadioGroup; importandroid.widget.TextView; importandroid.widget.Toast; pu

3、blic class RadioTest extends Activity /* Called when the activity is first created. */ TextViewtextview; RadioGroupradiogroup; RadioButton radio1,radio2,radio3,radio4; Override public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.main); textview

4、=(TextView)findViewById(R.id.textview1); radiogroup=(RadioGroup)findViewById(R.id.radiogroup1); radio1=(RadioButton)findViewById(R.id.radiobutton1); radio2=(RadioButton)findViewById(R.id.radiobutton2); radio3=(RadioButton)findViewById(R.id.radiobutton3); radio4=(RadioButton)findViewById(R.id.radiobu

5、tton4); radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener Override public void onCheckedChanged(RadioGroup group, intcheckedId) / TODO Auto-generated method stub if(checkedId=radio2.getId) DisplayToast(正确答案:+radio2.getText+,恭喜你,回答正确!); else DisplayToast(请注意,回答错误!); ); publ

6、ic void DisplayToast(String str) Toast toast=Toast.makeText(this, str, Toast.LENGTH_LONG); toast.setGravity(Gravity.TOP,0,220); toast.show; strings.xml文件 哪个城市美女多? 单选按钮测试 杭州 成都 重庆 苏州 main.xml文件:注意,这里面,4个RadioButton包含在RadioGroup中 运行结果如下: 假如我们选择杭州,会提示错误的Toast 再次选中成都后,会提示答案正确 这里就可以看到,单选按钮的使用效果,如果只是使用RadioButton的话,把配置文件中RadioGroup去掉,当然,要重新为每个单选按钮设置监听,这样,这个RadioButton就跟Button没有什么区别了,我们可以选中多个,所以要注意,单选按钮要和RadioGroup一起使用,才能够实现单选的功能。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号