android实验2界面设计基本组件.docx
- 文档编号:13445626
- 上传时间:2023-06-14
- 格式:DOCX
- 页数:12
- 大小:96.35KB
android实验2界面设计基本组件.docx
《android实验2界面设计基本组件.docx》由会员分享,可在线阅读,更多相关《android实验2界面设计基本组件.docx(12页珍藏版)》请在冰点文库上搜索。
android实验2界面设计基本组件
android实验2界面设计:
基本组件
id="@+id/name" android: layout_width="200sp" android: layout_height="wrap_content" android: hint="@string/inputuser"/> --在主布局添加文本框和密码框--> text="@string/password" android: layout_width="match_parent" android: layout_height="wrap_content"/> id="@+id/password" android: layout_width="200sp" android: layout_height="wrap_content" android: inputType="textPassword"/> --在主布局添加性别文本和复选框--> android: text="@string/sex" android: layout_width="wrap_content" android: layout_height="wrap_content"/> android: id="@+id/sex" android: orientation="horizontal" android: layout_width="wrap_content" android: layout_height="wrap_content"> --设置默认选择的是女--> android: id="@+id/man" android: text="@string/man" android: layout_width="wrap_content" android: layout_height="wrap_content"/> android: id="@+id/woman" android: checked="true" android: text="@string/woman" android: layout_width="wrap_content" android: layout_height="wrap_content"/> --在主布局添加联系电话文本框和输入框--> android: text="@string/phone" android: layout_width="wrap_content" android: layout_height="wrap_content"/> id="@+id/phone" android: inputType="text|phone" android: layout_width="200sp" android: layout_height="wrap_content"/> --在主布局中添加部门文本框和列表框--> android: text="@string/dept" android: layout_width="wrap_content" android: layout_height="wrap_content"/> id="@+id/dept" android: entries="@array/dept" android: layout_width="wrap_content" android: layout_height="wrap_content"/> --在主布局中添加爱好文本框和一个线性布局--> android: text="@string/hobby" android: layout_width="wrap_content" android: layout_height="wrap_content"/> layout_width="wrap_content" android: layout_height="wrap_content"> id="@+id/book" android: text="@string/book" android: layout_width="wrap_content" android: layout_height="wrap_content"/> id="@+id/sport" android: text="@string/sport" android: layout_width="wrap_content" android: layout_height="wrap_content"/> id="@+id/music" android: text="@string/music" android: layout_width="wrap_content" android: layout_height="wrap_content"/> id="@+id/movie" android: text="@string/movie" android: layout_width="wrap_content" android: layout_height="wrap_content"/> id="@+id/ok" android: layout_gravity="center_horizontal" android: text="@string/ok" android: onClick="myclick" android: layout_width="wrap_content" android: layout_height="wrap_content"/> (3)检测布局的后的效果 1.表示部门的Spinner组件,其数据来源文件depts.xml位于res/values目录下,代码如下所示 (1)编写数据文件 xmlversion="1.0"encoding="utf-8"? > (2)查看视图效果 2.设计后台程序 (1)在Activity文件中,定义局部中的组件对象和一个存放爱好中个复选对象的favs动态数组。 //1.用户名密码联系电话 privateEditTextuserName,phone; //2.性别 //性别的复选框 privateRadioGroupsex; //3.部门 privateSpinnerdept; //4.添加爱好的数组 privateArrayList //爱好 privateCheckBoxbook,sport,music,movie; (2)在onCreate()方法中,获取各组件。 //获取各组件 //1.获取用户名密码联系电话的实例对象 userName=(EditText)findViewById(R.id.name);//获取用户名的实例对象 //password=(EditText)findViewById(R.id.password);//获取密码的实力对象 phone=(EditText)findViewById(R.id.phone);//获取电话的实例对象 //2.获取性别的实例对象 sex=(RadioGroup)findViewById(R.id.sex); //man=(RadioButton)findViewById(R.id.man); //woman=(RadioButton)findViewById(R.id.woman); //3.获取部门的实例对象 dept=(Spinner)findViewById(R.id.dept); //4.获取爱好的实例对象 book=(CheckBox)findViewById(R.id.book); sport=(CheckBox)findViewById(R.id.sport); music=(CheckBox)findViewById(R.id.music); movie=(CheckBox)findViewById(R.id.movie); favs=newArrayList //将各项爱好存入数组 favs.add(book); favs.add(sport); favs.add(music); favs.add(movie); //5.获取按钮的实例对象 //submit=(Button)findViewById(R.id.ok); (3)获取性别方法。 //2.部分的一个方法,去获取一个性别的字符串返回出去 publicStringgetSex(){ //获取被电击了的按钮就是访问者的选项 RadioButtonradiobutton=(RadioButton)findViewById(sex.getCheckedRadioButtonId()); returnradiobutton.getText().toString(); } (4)获取爱好方法,爱好以逗号分隔。 //4.将爱好的信息形成一个字符串返货出去 publicStringgetFavorite(){ //临时的一个字符串获取爱好的信息 Stringfavo=""; for(CheckBoxcb: favs){ if(cb.isChecked()){ favo+=cb.getText().toString(); favo+=","; } } if(! "".equals(favo)){ favo=favo.substring(0,favo.length()-1);//去掉最后一个逗号 }else{ favo="您未选择爱好"; } returnfavo; } (5)当检查通过时,输出注册信息,提交到下一个Activity界面。 publicvoidmyclick(Viewview){ if(check()){ StringBuildersb=newStringBuilder(); //1.姓名密码电话 sb.append("用户名: "+userName.getText().toString()+"\n"); //sb.append("密码: "+password.getTextDirection()+"\n"); sb.append("电话: "+phone.getText().toString()+"\n"); //2.性别 sb.append("性别: "+getSex()+"\n");//自己写的私有方法 //3.部门 sb.append("部门: "+dept.getSelectedItem().toString()+"\n"); //4.爱好 sb.append("爱好: "+getFavorite().toString()+"\n"); Toast.makeText(this,sb.toString(),Toast.LENGTH_LONG).show(); Intentintent=newIntent();//将注册的信息提交到ResultActivity页面 intent.setClass(this,ResultActivity.class);//需要改进--------------------------- intent.putExtra("info",sb.toString()); this.startActivity(intent); } } (6)创建一个result_activity.xml布局文件,放置一个文本框组件,并创建ResultActivity类,修改onCreate方法,显示上页传来的数据。 a创建一个result_activity.xml布局文件 android=" android: layout_width="match_parent" android: layout_height="match_parent" android: orientation="vertical"> --主布局中添加文本框和输入框--> android: id="@+id/result" android: layout_width="match_parent" android: layout_height="wrap_content"/> b创建ResultActivity类,修改onCreate方法 publicclassResultActivityextendsActivity{ @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.result_activity); TextViewresult=(TextView)findViewById(R.id.result); result.setText("从前一个页面传过来的内容如何: \n\n"+this.getIntent().getStringExtra("info")); } } c在AndroidManifest.xml文件注册ResultActivity类 android: name=".ResultActivity" android: label="@string/app_name"> 五.总结 1.实验过程中遇到的问题及解决办法; 问题: 在将组件值不能提交到ResultActivity类。 答: 需要在在AndroidManifest.xml文件注册ResultActivity类 2.对设计及调试过程的心得体会。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- android 实验 界面设计 基本 组件