第3章 Android控件Widgets.ppt

  1. 1、本文档共127页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
3.3 技术准备 3.3.4 知识点4:复选框(CheckBox)和单选按钮(RadioButton) 在完成了对界面的布局之后,我们来看一下主程序中的实现,程序代码如代码清单3-11所示。 代码清单3-11 第三章\ CH3_4\src\sziit\practice\chapter3\CH3_4.java //包引入语句省略 public class CH3_4 extends Activity {//从Activity基类派生子类CH3_4 private RadioGroup radioGroup1; //定义私有RadioGroup对象 private RadioButton rbSex1; //定义私有单选按钮RadioButton对象 private RadioButton rbSex2;//定义私有单选按钮RadioButton对象 // 定义多选按钮CheckBox对象 private CheckBox cbSport; //定义私有CheckBox对象 private CheckBox cbTravelling; //定义私有CheckBox对象 private CheckBox cbReading; //定义私有CheckBox对象 private CheckBox cbOthers; //定义私有CheckBox对象 private EditText etName;// 定义私有编辑框对象 private Button btnOK;// 定义私有按钮对象 //定义字符串变量,userName、sex和hobby分别用来表示姓名、性别和爱好。 String userName; String sex=; String hobby=; public void onCreate(Bundle savedInstanceState) {//子类重写基类的onCreate方法 super.onCreate(savedInstanceState);//调用基类的onCreate方法 setContentView(R.layout.main);//设置布局文件,通过R.layout.main引用布局资源 //从布局文件中获取各个对象 radioGroup1=(RadioGroup)findViewById(R.id.radioGroup1);//引用布局中的RadioGroup rbSex1=(RadioButton)findViewById(R.id.male);//通过findViewById引用RadioButton rbSex2=(RadioButton)findViewById(R.id.female); //通过findViewById引用RadioButton cbSport=(CheckBox)findViewById(R.id.sports); //通过findViewById引用CheckBox cbTravelling=(CheckBox)findViewById(R.ling); //通过findViewById引用CheckBox cbReading=(CheckBox)findViewById(R.id.reading); //通过findViewById引用CheckBox cbOthers=(CheckBox)findViewById(R.id.others); //通过findViewById引用CheckBox btnOK=(Button)findViewById(R.id.submit); //通过findViewById引用Button etName=(EditText)findViewById(R.id.userName); //通过findViewById引用EditText //设置radioGroup1的事件监听 radioGroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){ public void onCheckedChanged(RadioGroup group, int checkedId) {//处理单击变更 if(checkedId==rbSex1.getId()){//通过Id识别RadioButton对象rbSex1 Toast.makeText(CH3_4.this, 性别:+rbSex1.getText()

文档评论(0)

***** + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档