(一)建立单选框按钮
RadioGroup和Radiobutton建立单选框按钮
字符串资源文件:
<resources> <string name="app_name">婚姻建议程序</string> <string name="sex">性别:</string> <string name="age">年龄:</string> <string name="btn_ok">确定</string> <string name="result">建议:</string> <string name="edt_age_hint">(输入年龄)</string> <string name="sug_not_hurry">还不急</string> <string name="sug_get_marrIEd">赶快结婚!</string> <string name="sug_find_couple">开始找对象。</string> <string name="male">男生</string> <string name="female">女生</string> <string name="male_age_range1">小于28岁</string> <string name="male_age_range2">28~33岁</string> <string name="male_age_range3">大于33岁</string> <string name="female_age_range1">小于25岁</string> <string name="female_age_range2">25~30岁</string> <string name="female_age_range3">大于30岁</string></resources>
界面布局文件:
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:app="http://schemas.androID.com/apk/res-auto" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="vertical" tools:context=".MainActivity" > <TextVIEw androID:ID="@+ID/sex" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:text="@string/sex" androID:textSize="25sp" /> <RadioGroup androID:ID="@+ID/radGrpSex" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:orIEntation="vertical" androID:checkedbutton="@+ID/radBtnMale"> <Radiobutton androID:ID="@+ID/radBtnMale" androID:textSize="20sp" androID:text="@string/male" /> <Radiobutton androID:ID="@+ID/radBtnFemale" androID:textSize="20sp" androID:text="@string/female" /> </RadioGroup> <TextVIEw androID:ID="@+ID/age" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:text="@string/age" androID:textSize="25sp" /> <RadioGroup androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:ID="@+ID/radGrpAge" androID:orIEntation="vertical" androID:checkedbutton="@+ID/radBtnAgeRange1"> <Radiobutton androID:ID="@+ID/radBtnAgeRange1" androID:textSize="20sp" androID:text="@string/male_age_range1"/> <Radiobutton androID:ID="@+ID/radBtnAgeRange2" androID:textSize="20sp" androID:text="@string/male_age_range2"/> <Radiobutton androID:ID="@+ID/radBtnAgeRange3" androID:textSize="20sp" androID:text="@string/male_age_range3"/> </RadioGroup> <button androID:ID="@+ID/btnOk" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_gravity="center_horizontal" androID:background="#4CAF50" androID:text="@string/btn_ok" /> <TextVIEw androID:ID="@+ID/txtR" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:text="@string/result" androID:textSize="25sp" /></linearLayout>
程序文件
package com.example.newapp;import androIDx.appcompat.app.AppCompatActivity;import androID.os.Bundle;import androID.vIEw.VIEw;import androID.Widget.AdapterVIEw;import androID.Widget.button;import androID.Widget.EditText;import androID.Widget.Radiobutton;import androID.Widget.RadioGroup;import androID.Widget.Spinner;import androID.Widget.TextVIEw;public class MainActivity extends AppCompatActivity { private button mBtnOk; private TextVIEw mTxtR; private RadioGroup mRadGrpSex,mRadGrpAge; private Radiobutton mRadBtnAgeRange1,mRadBtnAgeRange2,mRadBtnAgeRange3; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); mBtnOk=(button)findVIEwByID(R.ID.btnOk); mTxtR=(TextVIEw)findVIEwByID(R.ID.txtR); mBtnOk.setonClickListener(btnOkOnClick); mRadGrpAge=(RadioGroup)findVIEwByID(R.ID.radGrpAge); mRadGrpSex=(RadioGroup)findVIEwByID(R.ID.radGrpSex); mRadBtnAgeRange1=(Radiobutton)findVIEwByID(R.ID.radBtnAgeRange1); mRadBtnAgeRange2=(Radiobutton)findVIEwByID(R.ID.radBtnAgeRange2); mRadBtnAgeRange3=(Radiobutton)findVIEwByID(R.ID.radBtnAgeRange3); mRadGrpSex.setonCheckedchangelistener(radGrpSexOnCheckedChange); } private VIEw.OnClickListener btnOkOnClick =new VIEw.OnClickListener(){ @OverrIDe public voID onClick(VIEw v) { String strSug=getString(R.string.result); switch (mRadGrpAge.getCheckedRadiobuttonID()){ case R.ID.radBtnAgeRange1: strSug+=getString(R.string.sug_not_hurry); break; case R.ID.radBtnAgeRange2: strSug+=getString(R.string.sug_find_couple); break; case R.ID.radBtnAgeRange3: strSug+=getString(R.string.sug_get_marrIEd); break; } mTxtR.setText(strSug); } }; private RadioGroup.OnCheckedchangelistener radGrpSexOnCheckedChange = new RadioGroup.OnCheckedchangelistener() { @OverrIDe public voID onCheckedChanged(RadioGroup group, int checkedID) { if(checkedID==R.ID.radBtnMale){ mRadBtnAgeRange1.setText(getString(R.string.male_age_range1)); mRadBtnAgeRange2.setText(getString(R.string.male_age_range2)); mRadBtnAgeRange3.setText(getString(R.string.male_age_range3)); }else{ mRadBtnAgeRange1.setText(getString(R.string.female_age_range1)); mRadBtnAgeRange2.setText(getString(R.string.female_age_range2)); mRadBtnAgeRange3.setText(getString(R.string.female_age_range3)); } } };}
程序截图:
(二)下拉框Spinner和数字转轮NumberPicker的使用
Spinner组件来控制实现选择性别
数字转轮来实现年龄的选择
字符串资源文件:
<resources> <string name="app_name">婚姻建议程序</string> <string name="sex">性别:</string> <string name="age">年龄:</string> <string name="btn_ok">确定</string> <string name="result">建议:</string> <string name="edt_age_hint">(输入年龄)</string> <string name="sug_not_hurry">还不急</string> <string name="sug_get_marrIEd">赶快结婚!</string> <string name="sug_find_couple">开始找对象。</string> <string name="sex_male">男</string> <string-array name="sex_List"> <item>男</item> <item>女</item> </string-array> <string name="spn_sex_List_prompt">请选择性别</string></resources>
界面布局文件:
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:app="http://schemas.androID.com/apk/res-auto" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="vertical" tools:context=".MainActivity" > <TextVIEw androID:ID="@+ID/sex" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:text="@string/sex" androID:textSize="25sp" /> <Spinner androID:ID="@+ID/spnSex" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:entrIEs="@array/sex_List" androID:spinnerMode="dialog" androID:prompt="@string/spn_sex_List_prompt" /> <TextVIEw androID:ID="@+ID/age" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:text="@string/age" androID:textSize="25sp" /> <TextVIEw androID:ID="@+ID/txtAge" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:textSize="25sp" /> <NumberPicker androID:ID="@+ID/numPickerAge" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_gravity="center_horizontal" /> <button androID:ID="@+ID/btnOk" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_gravity="center_horizontal" androID:background="#4CAF50" androID:text="@string/btn_ok" /> <TextVIEw androID:ID="@+ID/txtR" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:text="@string/result" androID:textSize="25sp" /></linearLayout>
主程序文件:
package com.example.newapp;import androIDx.appcompat.app.AppCompatActivity;import androID.os.Bundle;import androID.vIEw.VIEw;import androID.Widget.AdapterVIEw;import androID.Widget.button;import androID.Widget.EditText;import androID.Widget.NumberPicker;import androID.Widget.Radiobutton;import androID.Widget.RadioGroup;import androID.Widget.Spinner;import androID.Widget.TextVIEw;public class MainActivity extends AppCompatActivity { private NumberPicker mNumPickerAge; private button mBtnOk; private TextVIEw mTxtR,mTxtAge; private Spinner mSpnSex; private String msSex; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); mTxtAge=(TextVIEw)findVIEwByID(R.ID.txtAge); mTxtAge.setText("25"); mNumPickerAge=(NumberPicker)findVIEwByID(R.ID.numPickerAge); mNumPickerAge.setMinValue(0); mNumPickerAge.setMaxValue(200); mNumPickerAge.setValue(25); mNumPickerAge.setonValueChangedListener(numPickerAgeOnValueChange); mBtnOk=(button)findVIEwByID(R.ID.btnOk); mTxtR=(TextVIEw)findVIEwByID(R.ID.txtR); mBtnOk.setonClickListener(btnOkOnClick); mSpnSex=(Spinner)findVIEwByID(R.ID.spnSex); mSpnSex.setonItemSelectedListener(spnSexOnItemSelected); } private VIEw.OnClickListener btnOkOnClick =new VIEw.OnClickListener(){ @OverrIDe public voID onClick(VIEw v) { int iAge=mNumPickerAge.getValue(); String strSug=getString(R.string.result); if(msSex.equals(getString(R.string.sex_male))) { if(iAge<28) strSug+=getString(R.string.sug_not_hurry); else if(iAge>33) strSug+=getString(R.string.sug_get_marrIEd); else strSug+=getString(R.string.sug_find_couple); } else { if(iAge<25) strSug+=getString(R.string.sug_not_hurry); else if(iAge>30) strSug+=getString(R.string.sug_get_marrIEd); else strSug+=getString(R.string.sug_find_couple); } mTxtR.setText(strSug); } }; private AdapterVIEw.OnItemSelectedListener spnSexOnItemSelected=new AdapterVIEw.OnItemSelectedListener() { @OverrIDe public voID onItemSelected(AdapterVIEw<?> parent, VIEw vIEw, int position, long ID) { msSex=parent.getSelectedItem().toString(); } @OverrIDe public voID onnothingSelected(AdapterVIEw<?> parent) { } }; private NumberPicker.OnValuechangelistener numPickerAgeOnValueChange=new NumberPicker.OnValuechangelistener() { @OverrIDe public voID onValueChange(NumberPicker picker, int oldVal, int newVal) { mTxtAge.setText(String.valueOf(newVal)); } };}
程序截图:
(三)复选框和滚动条(CheckBox与ScrollVIEw)
在进行多项选择时,由于项目过多我们可能会使用到滚动条,因此本次的实验目的是:实现用滚动条来上下滚动对项目的选择
同时使用上复选框
字符串资源文件:
<resources> <string name="app_name">兴趣选择程序</string> <string name="music">音乐</string> <string name="sing">唱歌</string> <string name="dance">跳舞</string> <string name="travel">旅行</string> <string name="reading">阅读</string> <string name="writing">写作</string> <string name="climbing">爬山</string> <string name="swim">游泳</string> <string name="exercise">运动</string> <string name="fitness">健身</string> <string name="photo">摄影</string> <string name="eating">美食</string> <string name="painting">绘画</string> <string name="your_hobby">您的兴趣:</string> <string name="btn_ok">确定</string></resources>
界面布局文件:
<?xml version="1.0" enCoding="utf-8"?><ScrollVIEw xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:app="http://schemas.androID.com/apk/res-auto" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content"> <linearLayout androID:ID="@+ID/linearLayout1" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:orIEntation="vertical" tools:context=".MainActivity"> <CheckBox androID:ID="@+ID/chkBoxMusic" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:textSize="30sp" androID:text="@string/music" /> <CheckBox androID:ID="@+ID/chkBoxSing" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:textSize="30sp" androID:text="@string/sing" /> <CheckBox androID:ID="@+ID/chkBoxDancing" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:textSize="30sp" androID:text="@string/dance" /> <CheckBox androID:ID="@+ID/chkBoxTravel" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:textSize="30sp" androID:text="@string/travel" /> <CheckBox androID:ID="@+ID/chkBoxReading" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:textSize="30sp" androID:text="@string/reading" /> <CheckBox androID:ID="@+ID/chkBoxWriting" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:textSize="30sp" androID:text="@string/writing" /> <CheckBox androID:ID="@+ID/chkBoxclimbing" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:textSize="30sp" androID:text="@string/climbing" /> <CheckBox androID:ID="@+ID/chkBoxSwim" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:textSize="30sp" androID:text="@string/swim" /> <CheckBox androID:ID="@+ID/chkBoxExercise" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:text="@string/exercise" androID:textSize="30sp" /> <CheckBox androID:ID="@+ID/chkBoxfitness" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:textSize="30sp" androID:text="@string/fitness" /> <CheckBox androID:ID="@+ID/chkBoxPhoto" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:textSize="30sp" androID:text="@string/photo" /> <CheckBox androID:ID="@+ID/chkBoxEating" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:textSize="30sp" androID:text="@string/eating" /> <CheckBox androID:ID="@+ID/chkBoxPainting" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:textSize="30sp" androID:text="@string/painting" /> <button androID:ID="@+ID/btnOk" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="@string/btn_ok" /> <TextVIEw androID:ID="@+ID/txtHobby" androID:layout_wIDth="420dp" androID:layout_height="47dp" /> </linearLayout></ScrollVIEw>
程序文件:
package com.example.newapp;import androIDx.appcompat.app.AppCompatActivity;import androID.os.Bundle;import androID.vIEw.VIEw;import androID.Widget.button;import androID.Widget.CheckBox;import androID.Widget.TextVIEw;import org.w3c.dom.Text;public class MainActivity extends AppCompatActivity { private CheckBox mChkBoxMusic,mChkBoxSing,mChkBoxDance,mChkBoxTravel,mChkBoxReading,mChkBoxWriting,mChkBoxfitness,mChkBoxclimbing,mChkBoxSwim,mChkBoxExercise,mChkBoxPhoto,mChkBoxEating,mChkBoxPainting; private button mBtnOk; private TextVIEw mTxtHobby; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); mChkBoxMusic=(CheckBox)findVIEwByID(R.ID.chkBoxMusic); mChkBoxSing=(CheckBox)findVIEwByID(R.ID.chkBoxSing); mChkBoxDance=(CheckBox)findVIEwByID(R.ID.chkBoxDancing); mChkBoxTravel=(CheckBox)findVIEwByID(R.ID.chkBoxTravel); mChkBoxReading=(CheckBox)findVIEwByID(R.ID.chkBoxReading); mChkBoxWriting=(CheckBox)findVIEwByID(R.ID.chkBoxWriting); mChkBoxclimbing=(CheckBox)findVIEwByID(R.ID.chkBoxclimbing); mChkBoxSwim=(CheckBox)findVIEwByID(R.ID.chkBoxSwim); mChkBoxExercise=(CheckBox)findVIEwByID(R.ID.chkBoxExercise); mChkBoxfitness=(CheckBox)findVIEwByID(R.ID.chkBoxfitness); mChkBoxPhoto=(CheckBox)findVIEwByID(R.ID.chkBoxPhoto); mChkBoxEating=(CheckBox)findVIEwByID(R.ID.chkBoxEating); mChkBoxPainting=(CheckBox)findVIEwByID(R.ID.chkBoxPainting); mBtnOk=(button)findVIEwByID(R.ID.btnOk); mTxtHobby=(TextVIEw)findVIEwByID(R.ID.txtHobby); mBtnOk.setonClickListener(btnOkOnClick); } private VIEw.OnClickListener btnOkOnClick=new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { String s=getString(R.string.your_hobby); if(mChkBoxMusic.isChecked()) s+=mChkBoxMusic.getText().toString(); if(mChkBoxSing.isChecked()) s+=mChkBoxSing.getText().toString(); if(mChkBoxDance.isChecked()) s+=mChkBoxDance.getText().toString(); if(mChkBoxTravel.isChecked()) s+=mChkBoxTravel.getText().toString(); if(mChkBoxReading.isChecked()) s+=mChkBoxReading.getText().toString(); if(mChkBoxWriting.isChecked()) s+=mChkBoxWriting.getText().toString(); if(mChkBoxclimbing.isChecked()) s+=mChkBoxclimbing.getText().toString(); if(mChkBoxSwim.isChecked()) s+=mChkBoxSwim.getText().toString(); if(mChkBoxExercise.isChecked()) s+=mChkBoxExercise.getText().toString(); if(mChkBoxfitness.isChecked()) s+=mChkBoxfitness.getText().toString(); if(mChkBoxPhoto.isChecked()) s+=mChkBoxPhoto.getText().toString(); if(mChkBoxEating.isChecked()) s+=mChkBoxEating.getText().toString(); if(mChkBoxPainting.isChecked()) s+=mChkBoxPainting.getText().toString(); mTxtHobby.setText(s); } };}
程序截图:
总结
以上是内存溢出为你收集整理的Android学习使用基本界面组件(下拉框,单选框,复选框,数字转轮,滚动条)全部内容,希望文章能够帮你解决Android学习使用基本界面组件(下拉框,单选框,复选框,数字转轮,滚动条)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)