Android_baseComponentExample
2016-07-29 11:44
316 查看
xml布局文件:
源代码:
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="学号:" android:textSize="20sp" /> <AutoCompleteTextView android:id="@+id/actv_number" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="5" android:hint="请输入学号" android:inputType="number" android:completionThreshold="2"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密码:" android:textSize="20sp"/> <EditText android:id="@+id/et_psd" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入密码" android:textSize="20sp" android:inputType="textPassword"/> </LinearLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" > <TextView android:id="@+id/hobby" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="爱好:" android:textSize="20sp" /> <CheckBox android:id="@+id/cb_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/hobby" android:layout_alignParentLeft="true" android:text="唱歌" android:checked="true" android:textSize="15sp"/> <CheckBox android:id="@+id/cb_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/hobby" android:layout_toRightOf="@id/cb_1" android:layout_marginLeft="5dp" android:text="打球" android:checked="true" android:textSize="15sp"/> <CheckBox android:id="@+id/cb_3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/hobby" android:layout_toRightOf="@id/cb_2" android:layout_marginLeft="5dp" android:text="玩游戏" android:textSize="15sp"/> <CheckBox android:id="@+id/cb_4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/hobby" android:layout_toRightOf="@id/cb_3" android:text="敲代码" android:textSize="15sp" /> <CheckBox android:id="@+id/cb_all" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="全选" android:textSize="15sp" android:layout_below="@id/cb_1" android:layout_centerHorizontal="true"/> </RelativeLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="性别:" android:textSize="20sp" /> <Spinner android:id="@+id/sex" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/commit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom" android:text="提交" android:textSize="15sp" /> <Button android:id="@+id/reset" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom" android:text="重置" android:textSize="15sp" /> </LinearLayout> </LinearLayout>
源代码:
packagecom.example.day04_work; importjava.util.ArrayList; importjava.util.List; importandroid.os.Bundle; importandroid.app.Activity; importandroid.view.View; importandroid.view.View.OnClickListener; importandroid.widget.AdapterView; importandroid.widget.ArrayAdapter; importandroid.widget.AutoCompleteTextView; importandroid.widget.Button; importandroid.widget.CheckBox; importandroid.widget.CompoundButton; importandroid.widget.Spinner; importandroid.widget.Toast; importandroid.widget.CompoundButton.OnCheckedChangeListener; importandroid.widget.EditText; publicclassMainActivityextendsActivityimplementsOnClickListener,OnCheckedChangeListener{ privateEditTextet_psd; privateCheckBoxcb_1; privateCheckBoxcb_2; privateCheckBoxcb_3; privateCheckBoxcb_4; privateCheckBoxcb_all; privateStudentstudent=newStudent(); privateButtonreset; privateButtoncommit; privateList<CheckBox>check1; privateStringsrcnumber; privateStringsrcpsd; privateAutoCompleteTextViewactv_number; privateCheckBox[]checks; privateSpinnerspinner_sex; privateStringsex; @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); actv_number=(AutoCompleteTextView)findViewById(R.id.actv_number); //得到数据源 String[]stringArray=getResources().getStringArray(R.array.number); //定义适配器 ArrayAdapter<String>adapter=newArrayAdapter<String>(MainActivity.this,android.R.layout.simple_dropdown_item_1line,stringArray); //设置适配器 actv_number.setAdapter(adapter); //得到控件 et_psd=(EditText)findViewById(R.id.et_psd); cb_1=(CheckBox)findViewById(R.id.cb_1); cb_2=(CheckBox)findViewById(R.id.cb_2); cb_3=(CheckBox)findViewById(R.id.cb_3); cb_4=(CheckBox)findViewById(R.id.cb_4); cb_all=(CheckBox)findViewById(R.id.cb_all); reset=(Button)findViewById(R.id.reset); commit=(Button)findViewById(R.id.commit); spinner_sex=(Spinner)findViewById(R.id.sex); ArrayAdapter<String>adapter2=newArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_dropdown_item_1line,newString[]{"男","女"}); spinner_sex.setAdapter(adapter2); //得到初始化时的所有数据 getInitialContent(); //为所有控件设置监听事件 actv_number.setOnClickListener(this); et_psd.setOnClickListener(this); cb_1.setOnCheckedChangeListener(this); cb_2.setOnCheckedChangeListener(this); cb_3.setOnCheckedChangeListener(this); cb_4.setOnCheckedChangeListener(this); cb_all.setOnCheckedChangeListener(this); //提交按钮监听事件 commit.setOnClickListener(newOnClickListener(){ @Override publicvoidonClick(Viewv){ Stringnumber=actv_number.getText().toString(); Stringpsd=et_psd.getText().toString(); if(number.length()!=0&&psd.length()!=0){ student.setNumber(Integer.parseInt(number)); student.setPsd(psd); }else{ Toast.makeText(MainActivity.this,"学号密码不能为空!",0).show(); return; } Stringresult=""; //TODOAuto-generatedmethodstub for(CheckBoxcheckBox:checks){ if(checkBox.isChecked()){ result+=checkBox.getText().toString(); } } //安卓自带工具TextUtilstextUtils判断字符串是否为空 //TextUtils.isEmpty(string) if(result.length()!=0){ student.setHobby(result); }else{ Toast.makeText(MainActivity.this,"爱好不能为空",0).show(); } student.setSex(sex); Toast.makeText(MainActivity.this,student.toString(),1).show(); } }); //重置按钮监听事件 reset.setOnClickListener(newOnClickListener(){ @Override publicvoidonClick(Viewv){ //TODOAuto-generatedmethodstub et_psd.setText(srcpsd); //学号获得焦点,光标在学号输入框上 actv_number.requestFocus(); actv_number.setText(srcnumber); //将初始化复选框数组中的控件设置为选中 for(CheckBoxcheckBox2:check1){ checkBox2.setChecked(true); } //遍历所有复选框,如果初始化复选框控件中不包含该复选框,设置为不选中 for(CheckBoxcheckBox:checks){ if(!check1.contains(checkBox)){ checkBox.setChecked(false); } } spinner_sex.setSelection(0,true); } }); //下拉列表选择性别 spinner_sex.setOnItemSelectedListener(newAdapterView.OnItemSelectedListener(){ @Override publicvoidonItemSelected(AdapterView<?>parent,Viewview, intposition,longid){ sex=spinner_sex.getItemAtPosition(position).toString(); } @Override publicvoidonNothingSelected(AdapterView<?>parent){ //TODOAuto-generatedmethodstub } }); } /** *得到初始化数据 */ privatevoidgetInitialContent(){ //TODOAuto-generatedmethodstub srcnumber=actv_number.getText().toString(); srcpsd=et_psd.getText().toString(); checks=newCheckBox[]{cb_1,cb_2,cb_3,cb_4,cb_all}; check1=newArrayList<CheckBox>(); for(CheckBoxcheckBox:checks){ if(checkBox.isChecked()){ check1.add(checkBox); } } spinner_sex.setSelection(0,true); } /** 复选框监听事件: *全选功能: *1.全选状态改变: *1.1全选框选中时,所有复选框都选中 *1.2全选框取消时,如果所有复选框都为选中状态,则将所有复选框设置为不选中 *2.其他复选框选中时: *2.1当所有复选框选中时,将全选框设置为选中 *2.2否则,将全选框设置为不选中 */ @Override publicvoidonCheckedChanged(CompoundButtonbuttonView,booleanisChecked){ List<String>list=newArrayList<String>(); //TODOAuto-generatedmethodstub if(buttonView.getId()==cb_all.getId()){ if(cb_all.isChecked()){ cb_1.setChecked(true); cb_2.setChecked(true); cb_3.setChecked(true); cb_4.setChecked(true); }elseif(cb_1.isChecked()&&cb_2.isChecked()&&cb_3.isChecked()&&cb_4.isChecked()){ cb_1.setChecked(false); cb_2.setChecked(false); cb_3.setChecked(false); cb_4.setChecked(false); } }else{ if(cb_1.isChecked()&&cb_2.isChecked()&&cb_3.isChecked()&&cb_4.isChecked()){ cb_all.setChecked(true); }else{ cb_all.setChecked(false); } } } @Override publicvoidonClick(Viewv){ //TODOAuto-generatedmethodstub Stringnumber=actv_number.getText().toString(); Stringpsd=et_psd.getText().toString(); if(number!=null&&psd!=null){ Toast.makeText(MainActivity.this,"学号:"+number+",密码"+psd,0).show(); } } }
相关文章推荐
- traceView的使用详解
- android 带与不带logo的二维码生成
- Fragment滑动加解析
- 基于Android Studio的内存泄漏检测与解决
- 零散知识点
- 如何适配屏幕以及 android 设备是如何寻找到相对于的resources
- Android开发之Fragment传递参数的几种方法
- 【android】安卓手机连接电脑时候adb devices找不到设备及找到设备但无权限的问题
- android studio 获取安卓程序版本号不正确
- Android Studio 真机不能调试,报错Error:java.lang.UnsupportedClassVersionError: com/android/dx/command/Main :
- Android之DDMS(Dalvik Debug Monitor Service)
- android:通过选择相册或者拍照获取照片
- Android - Android Studio 加速Gradle Bulid 速度
- Android6.0权限管理之开源项目Grant
- Android 软件自动更新功能实现的方法
- Windows平台上Android NDK初试用
- Android Glide图片加载库的使用
- Android渐变GradientDrawable叠加组合环ring
- Android渐变GradientDrawable叠加组合环ring
- 使用RxPermission框架对android6.0权限进行检测