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. 有关SQL

    1.SQL str函数是什么意思? 将数值型转换成指定长度的字符串.str()函数语法:str(数字类型的表达式[,表达式总长度][,小数点后面的位数]),表达式总长度和小数点后面的位数为可选择参数. ...

  2. POJ 1155-TELE(树形背包)

    题意:电视台发送信号给很多用户,每个用户(叶子节点)有愿意出的钱,电视台经过的路线都有一定费用,求电视台不损失的情况下最多给多少用户发送信号. 分析:问题与以i为根节点的子树所包含的叶子数 #incl ...

  3. HDU 5622 KK's Chemical DP

    题意:bc round 71(中文题面) 分析(官方题解): 根据药品之间的相互关系,我们可以构建一张图,我们对相互会发生反应的药品连边 这个图的特征,是一个环加上一些“树”(可能有多个联通块) 一个 ...

  4. 关于Aggregate 的一点用法

    比如 我们要 将数组或者对象中的某列或某属性 的值取出,然后 用 逗号隔开. 1.通常我们可能会用for 或foreach 来循环,然后将取出的值并添加至StringBuilder 2.用Aggreg ...

  5. 6.1 CUDA: pinned memory固定存储

    CPU和GPU内存交互 在CUDA编程中,内存拷贝是非常费时的一个动作. 从上图我们可以看出:1. CPU和GPU之间的总线bus是PCIe,是双向传输的. 2. CPU和GPU之间的数据拷贝使用DM ...

  6. HW6.9

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...

  7. 一切不以用户为中心的O2O 都是耍流氓

    今天去万达广场逛街,手机搜了下附近的Wifi,发现有万达的免费Wifi,想起前些日子网上说万达要做O2O的试运营,于是尝试连接了下,结果弹出页面,要输入手机号,然后发送验证码才能登陆,结果输入手机号, ...

  8. Head First设计模式-观察者模式

    一.整体代码 Subject.java public interface Subject { public void registerObserver(Observer o); public void ...

  9. Android项目实战--手机卫士24--程序锁的实现以及逻辑

    昨天我们已经把程序全部读取出来,显示到界面上了,那今天,我们就来讲一下那个程序锁的实现啦.其实也很简单啦,我们主要就是把用户要锁定的程序放到一个表里面,然后就监听手机里面的任务栈,如果发现任务栈里面出 ...

  10. jquery完成带复选框的表格行高亮显示

    jquery完成带复选框的表格行高亮显示 通过jquery技术来操作表格是件简单的事,通过jquery的语法,可以很轻松的完成表格的隔行换色,悬浮高亮,在实际的应用中可能会出现表格中带复选框的,删除时 ...