xml布局文件:

<LinearLayout xmlns: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>

源代码:

package com.example.day04_work;

import java.util.ArrayList;
import java.util.List; import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
public class MainActivity extends Activity implements OnClickListener,OnCheckedChangeListener{ private EditText et_psd;
private CheckBox cb_1;
private CheckBox cb_2;
private CheckBox cb_3;
private CheckBox cb_4;
private CheckBox cb_all;
private Student student = new Student();
private Button reset;
private Button commit;
private List<CheckBox> check1;
private String srcnumber;
private String srcpsd;
private AutoCompleteTextView actv_number;
private CheckBox[] checks;
private Spinner spinner_sex;
private String sex; @Override
protected void onCreate(Bundle savedInstanceState) {
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 = new ArrayAdapter<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 = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_dropdown_item_1line,new String[]{"男","女"});
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(new OnClickListener() { @Override
public void onClick(View v) {
String number = actv_number.getText().toString();
String psd = 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;
}
String result = "";
// TODO Auto-generated method stub
for (CheckBox checkBox : checks) {
if(checkBox.isChecked()){
result +=checkBox.getText().toString();
}
}
//安卓自带工具 TextUtils textUtils判断字符串是否为空
//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(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
et_psd.setText(srcpsd);
//学号获得焦点,光标在学号输入框上
actv_number.requestFocus();
actv_number.setText(srcnumber);
//将初始化复选框数组中的控件设置为选中
for (CheckBox checkBox2 : check1) {
checkBox2.setChecked(true);
}
//遍历所有复选框,如果初始化复选框控件中不包含该复选框,设置为不选中
for (CheckBox checkBox : checks) {
if(!check1.contains(checkBox)){
checkBox.setChecked(false);
}
}
spinner_sex.setSelection(0, true);
} });
//下拉列表选择性别
spinner_sex.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
sex = spinner_sex.getItemAtPosition(position).toString(); } @Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub }
}) ; }
/**
* 得到初始化数据
*/
private void getInitialContent() {
// TODO Auto-generated method stub
srcnumber = actv_number.getText().toString();
srcpsd = et_psd.getText().toString();
checks = new CheckBox[] {cb_1,cb_2,cb_3,cb_4,cb_all};
check1 = new ArrayList<CheckBox>();
for (CheckBox checkBox : checks) {
if(checkBox.isChecked()){
check1.add(checkBox);
}
}
spinner_sex.setSelection(0, true);
} /**
复选框监听事件:
* 全选功能:
* 1.全选状态改变:
* 1.1 全选框选中时,所有复选框都选中
* 1.2全选框取消时,如果所有复选框都为选中状态,则将所有复选框设置为不选中
* 2.其他复选框选中时:
* 2.1当所有复选框选中时,将全选框设置为选中
* 2.2否则,将全选框设置为不选中
*/
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
List<String> list = new ArrayList<String>();
// TODO Auto-generated method stub
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);
}else if(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
public void onClick(View v) {
// TODO Auto-generated method stub
String number = actv_number.getText().toString();
String psd = et_psd.getText().toString();
if(number!=null && psd != null){
Toast.makeText(MainActivity.this, "学号:"+number+",密码"+psd, 0).show();
}
} }
 

Android_baseComponentExample的更多相关文章

随机推荐

  1. 【原创】LoadRunner Java Vuser脚本的配置和调试指南

    1 编写目的 本文介绍了Loadrunner多负载压力机的配置,并通过测试Java Vuser的数据库连接脚本对配置结果进行了验证,同时对配置过程中遇到的问题和解决的过程进行了记录,关于Java数据库 ...

  2. Android学习笔记:TabHost 和 FragmentTabHost

    TabHost 命名空间: android.widget.TabHost 初始化函数(必须在addTab之前调用): setup(); 包含两个子元素: 1.Tab标签容器TabWidget(@and ...

  3. 设计模式_Facade_门面模式

    形象例子: 我有一个专业的Nikon相机,我就喜欢自己手动调光圈.快门,这样照出来的照片才专业,但MM可不懂这些,教了半天也不会.幸好相机有Facade设计模式,把相机调整到自动档,只要对准目标按快门 ...

  4. 《Oracle Database 12c DBA指南》第二章 - 安装Oracle和创建数据库(2.1 安装Oracle数据库软件和创建数据库概览)

    当前关于12c的中文资料比较少,本人将关于DBA的一部分官方文档翻译为中文,很多地方为了帮助中国网友看懂文章,没有按照原文句式翻译,翻译不足之处难免,望多多指正. 2.1 安装Oracle数据库软件和 ...

  5. 【暑假】[实用数据结构]KMP

    KMP算法 KMP算法是字符串匹配算法,可以在O(n)的时间完成,算法包含两部分,分别是:构造适配函数与两串匹配. 失配边的使用大大提高了算法效率,可以理解为已经成功匹配的字符不在重新匹配,因为我们已 ...

  6. ROW_NUMBER 使用

    WITH t_pageAS( SELECT ROW_NUMBER() OVER ( ORDER BY table_name ) AS row_index,column_name FROM table_ ...

  7. ffmpeg常见命令

    一.安装       下载ffmpeg,解压之后配置环境变量即为安装 打开dos界面,进入目标文件夹例如:E:/ cd E:\BaiduYunDownload\ffmpeg\ffmpeg_simple ...

  8. Hander

    多线程与UI线程间通信 向你展示如何从任务发送数据对象上运行用户界面(UI)线程.该特性允许你的任务做背景的工作结果,然后再到UI元素如位图. 每个应用程序都有自己的特殊的线程运行的UI对象如视图对象 ...

  9. HIbernate学习笔记(五) 关系映射之一对多与多对一

    三.       多对一 –单向 场景:用户和组:从用户角度来,多个用户属于一个组(多对一 关联) 使用hibernate开发的思路:先建立对象模型(领域模型),把实体抽取出来. 目前两个实体:用户和 ...

  10. BAT及各大互联网公司2014前端笔试面试题:HTML/CSS篇

    BAT及各大互联网公司2014前端笔试面试题:HTML/CSS篇 2014/08/03 · Web前端, 开发 · CSS, HTML, 技术面试 分享到: 188 MongoDB集群之分片技术应用 ...