简单的注册提交

简单的注册提交

简单的注册提交

简单的注册提交

简单的注册提交
java代码
public class MainActivity extends Activity {

	private static final String[] cities={"北京","上海","武汉","广州","深圳"};
	private EditText name,age,pass;
	private Button regButton;
	private RadioGroup sexRadioGroup;
	private CheckBox basketball,football,pingpang,tennis;
	private Spinner cityItems;

	private boolean flag=true;
	private List<CheckBox> favorities;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		//定义一个ArrayList来存放所有的CheckBox
		favorities=new ArrayList<CheckBox>();

		//得到相应的显示控件的对象
		name = (EditText) findViewById(R.id.nameValue);
		age = (EditText) findViewById(R.id.ageValue);
		pass = (EditText) findViewById(R.id.passValue);
		regButton = (Button) findViewById(R.id.registerButton);
		cityItems = (Spinner) findViewById(R.id.cityItems);
		sexRadioGroup = (RadioGroup) findViewById(R.id.setRadioGroup);

		basketball = (CheckBox) findViewById(R.id.cb_lanqiu);
		//添加到favorities中
		favorities.add(basketball);
		football = (CheckBox) findViewById(R.id.cb_zuqiu);
		favorities.add(football);
		pingpang = (CheckBox) findViewById(R.id.cb_pingpang);
		favorities.add(pingpang);
		tennis = (CheckBox) findViewById(R.id.cb_wangqiu);
		favorities.add(tennis);

		//创建一个数组型适配器
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(
				this, android.R.layout.simple_spinner_item, cities);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		cityItems.setAdapter(adapter);

		//为regButton注册监听事件
		regButton.setOnClickListener(new OnClickListener() {
			//按钮被点击时调用该方法
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				flag=addUser();
				if(flag){
					new AlertDialog.Builder(MainActivity.this)
					.setTitle("请确认信息").setMessage(
							"你的信息如下:\n姓名:"+name.getText().toString()
							+"\n年龄:"+age.getText().toString()+"\n性别:"
							+getSex()+"\n爱好:"+getFavorite()+"\n城市:"
							+getCity()+"\n")
							.setCancelable(false).setPositiveButton("确定", 
									new DialogInterface.OnClickListener() {
										
										@Override
										public void onClick(DialogInterface dialog, int which) {
											ProgressDialog.show(MainActivity.this, 
													"用户信息注册中", "请等待......")
													.setCancelable(true);	
										}
									}).setNegativeButton("修改", 
											new DialogInterface.OnClickListener() {
										
										@Override
										public void onClick(DialogInterface dialog, int which) {
											//删除对话框
											dialog.cancel();
										}
									}).show();//显示对话框
				}
			}
		});

	}
	//获取Spinner中的值
	private String getCity(){
		return cities[cityItems.getSelectedItemPosition()];
	}
	//获取chenkBook的值
	private String getFavorite(){
		String favString="";
		for(CheckBox cb : favorities){
			if(cb.isChecked()){
				favString += cb.getText().toString();
				favString += ",";	
			}
		}
		if(favString !=""){
			favString = favString.substring(0, favString.length()-1);
		}else{
			favString = "你没有选择爱好!";
		}
		
		return favString;
	}
	
	//获取一组RadioGroup中被选中的RadioFroup的值
	private String getSex(){
		RadioButton mRadio = (RadioButton) findViewById(sexRadioGroup.getCheckedRadioButtonId());
		return mRadio.getText().toString();
	}
	
	//检测输入内容是否符合要求
	public boolean addUser(){
		if(name.getText().toString().length() == 0){
			name.setError("用户名不能为空");
			return false;
		}
		if(age.getText().toString().length() == 0){
			age.setError("年龄不能为空");
			return false;
		}
		if(pass.getText().toString().length() == 0){
			pass.setError("密码不能为空");
		}
		return true;
	}


界面代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/name"
            android:layout_width="90px"
            android:layout_height="wrap_content"
            android:text="用户名:"
            android:textSize="22px" />

        <EditText
            android:id="@+id/nameValue"
            android:layout_width="200px"
            android:layout_height="wrap_content" />
    </TableRow>

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/pass"
            android:layout_width="90px"
            android:layout_height="wrap_content"
            android:text="密码:"
            android:textSize="22px" />

        <EditText
            android:id="@+id/passValue"
            android:layout_width="200px"
            android:layout_height="wrap_content"
            android:password="true" />
    </TableRow>

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/age"
            android:layout_width="90px"
            android:layout_height="wrap_content"
            android:text="年龄:"
            android:textSize="22px" />

        <EditText
            android:id="@+id/ageValue"
            android:layout_width="200px"
            android:layout_height="wrap_content"
            android:numeric="integer" />
    </TableRow>

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/sex"
            android:layout_width="90px"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:text="性别:"
            android:textSize="22px" />

        <RadioGroup
            android:id="@+id/setRadioGroup"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checkedButton="@+id/radioMan"
            android:orientation="horizontal" >

            <RadioButton
                android:id="@+id/radioMan"
                android:text="男" />

            <RadioButton
                android:id="@+id/radioWoman"
                android:text="女" />
        </RadioGroup>
    </TableRow>

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/favorite"
            android:layout_width="90px"
            android:layout_height="wrap_content"
            android:text="爱好:"
            android:textSize="22px" />

        <CheckBox
            android:id="@+id/cb_pingpang"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/favorite"
            android:text="乒乓球" />

        <CheckBox
            android:id="@+id/cb_zuqiu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/cb_pingpang"
            android:text="足球" />

        <CheckBox
            android:id="@+id/cb_lanqiu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/cb_zuqiu"
            android:layout_toRightOf="@id/favorite"
            android:text="蓝球" />

        <CheckBox
            android:id="@+id/cb_wangqiu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/cb_zuqiu"
            android:layout_toRightOf="@id/cb_lanqiu"
            android:text="网球" />
    </RelativeLayout>

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/city"
            android:layout_width="90px"
            android:layout_height="wrap_content"
            android:text="城市:"
            android:textSize="22px" />

        <Spinner
            android:id="@+id/cityItems"
            android:layout_width="fill_parent"
            android:layout_height="50dp" />
    </TableRow>

    <Button
        android:id="@+id/registerButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="注册" />


</LinearLayout>

你可能感兴趣的:(android)