利用Android常用UI控件完成简单地注册功能

        最近在学习Android,刚刚看到UI控件这里,自己写了个小例子和初学者分享一下。

        实现的是一个注册页面,需要填写用户名(EditText),密码(EditText),年龄(EditText),性别(RadioButton),爱好(CheckBox),城市(Spinner)。其中用户名,密码,年龄不能为空,需要有验证;点击注册按钮后弹出一个确认窗口(AlertDialog),窗口中显示确认的信息(也就是刚才填写的信息),并且有确认和取消键;点击确认信息4秒后可以完成注册,过程和结果都要有提示。

        具体效果如下所示:



        具体代码实现如下:

regist.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <RelativeLayout 
        android:layout_width="match_parent"
    	android:layout_height="wrap_content">
        <TextView 
            android:id="@+id/name"
            android:layout_width="@dimen/textView"
    		android:layout_height="wrap_content"
    		android:text="@string/name"/>
        <EditText 
            android:id="@+id/nameValue"
            android:inputType="text"
            android:layout_toRightOf="@id/name"
            android:layout_toEndOf="@id/name"
            android:layout_width="@dimen/editText"
    		android:layout_height="wrap_content"/>
    </RelativeLayout>
     
    <RelativeLayout 
        android:layout_width="match_parent"
    	android:layout_height="wrap_content">
        <TextView 
            android:id="@+id/password"
            android:layout_width="@dimen/textView"
    		android:layout_height="wrap_content"
    		android:text="@string/password"/>
        <EditText 
            android:id="@+id/passwordValue"
            android:inputType="textPassword"
            android:layout_toRightOf="@id/password"
            android:layout_toEndOf="@id/password"
            android:layout_width="@dimen/editText"
    		android:layout_height="wrap_content"/>
    </RelativeLayout>
    
	<RelativeLayout 
        android:layout_width="match_parent"
    	android:layout_height="wrap_content">
        <TextView 
            android:id="@+id/age"
            android:layout_width="@dimen/textView"
    		android:layout_height="wrap_content"
    		android:text="@string/age"/>
        <EditText 
            android:id="@+id/ageValue"
            android:inputType="number"
            android:layout_toRightOf="@id/age"
            android:layout_toEndOf="@id/age"
            android:layout_width="@dimen/editText"
    		android:layout_height="wrap_content"/>
    </RelativeLayout>
    
	<RelativeLayout 
	    android:layout_width="match_parent"
    	android:layout_height="wrap_content">
	    <TextView 
            android:id="@+id/sex"
            android:layout_width="@dimen/textView"
    		android:layout_height="wrap_content"
    		android:text="@string/sex"/>
	    <RadioGroup 
	        android:id="@+id/sexMenu"
            android:layout_width="wrap_content"
    		android:layout_height="wrap_content"
    		android:layout_toRightOf="@id/sex"
    		android:layout_toEndOf="@id/sex"
    		android:checkedButton="@+id/man"
    		android:orientation="horizontal">
	        <RadioButton 
	            android:id="@id/man"
            	android:layout_width="wrap_content"
    			android:layout_height="wrap_content"
    			android:text="@string/man"/>
	        <RadioButton 
	            android:id="@+id/woman"
            	android:layout_width="wrap_content"
    			android:layout_height="wrap_content"
    			android:text="@string/woman"/>	        
	    </RadioGroup>
	</RelativeLayout>
	
	<RelativeLayout 
	    android:layout_width="match_parent"
    	android:layout_height="wrap_content">
	    <TextView 
            android:id="@+id/favorite"
            android:layout_width="@dimen/textView"
    		android:layout_height="wrap_content"
    		android:text="@string/favorite"/>
	    <CheckBox 
	        android:id="@+id/pingpang"
	        android:layout_width="wrap_content"
    		android:layout_height="wrap_content"
    		android:layout_toRightOf="@id/favorite"
    		android:layout_toEndOf="@id/favorite"
    		android:text="@string/pingpang"/>
	    <CheckBox 
	        android:id="@+id/football"
	        android:layout_width="wrap_content"
    		android:layout_height="wrap_content"
    		android:layout_toRightOf="@id/pingpang"
    		android:layout_toEndOf="@id/pingpang"
    		android:text="@string/football"/>
	    <CheckBox 
	        android:id="@+id/badminton"
	        android:layout_width="wrap_content"
    		android:layout_height="wrap_content"
    		android:layout_toRightOf="@id/favorite"
    		android:layout_toEndOf="@id/favorite"
    		android:layout_below="@id/football"
    		android:text="@string/badminton"/>
	    <CheckBox 
	        android:id="@+id/basketball"
	        android:layout_width="wrap_content"
    		android:layout_height="wrap_content"
    		android:layout_toRightOf="@id/badminton"
    		android:layout_toEndOf="@id/badminton"
    		android:layout_below="@id/football"
    		android:text="@string/basketball"/>
	</RelativeLayout>
	
	<RelativeLayout 
	    android:layout_width="match_parent"
    	android:layout_height="wrap_content">
	    <TextView 
            android:id="@+id/city"
            android:layout_width="@dimen/textView"
    		android:layout_height="wrap_content"
    		android:text="@string/city"/>
	    <Spinner 
	        android:id="@+id/cityValue"
	        android:layout_width="match_parent"
    		android:layout_height="wrap_content"
    		android:layout_toRightOf="@id/city"
    		android:layout_toEndOf="@id/city"/>
	</RelativeLayout>
	
	<Button 
	    android:id="@+id/regist"
	    android:layout_width="wrap_content"
    	android:layout_height="wrap_content"
    	android:text="@string/regist"/>
</LinearLayout>
strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">猴赛累</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    <string name="name">用户名:</string>
    <string name="password">密码:</string>
    <string name="age">年龄:</string>
    <string name="sex">性别:</string>
    <string name="favorite">爱好:</string>
    <string name="city">城市:</string>
    <string name="regist">注册</string>
    <string name="man">男</string>
    <string name="woman">女</string>
    <string name="pingpang">乒乓球</string>
    <string name="football">足球</string>
    <string name="badminton">羽毛球</string>
    <string name="basketball">篮球</string>

</resources>


dimens.xml

<resources>

    <!-- Default screen margins, per the Android Design guidelines. -->
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>
    <dimen name="textView">70dp</dimen>
    <dimen name="editText">200dp</dimen>

</resources>

MainActivity.java

package com.example.housailei;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends Activity {
	
	private boolean flag=true;
	private static final String[] citis={"济南","北京","上海","成都","广州","天津","杭州","深圳"};
	private EditText name,password,age;
	private RadioGroup sex;
	private List<CheckBox> favorites;
	private CheckBox pingpang,football,badminton,basketball;
	private Spinner city;
	private Button regist;
	private ProgressDialog progressDialog; 
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.regist);
		
		favorites=new ArrayList<CheckBox>();
		name=(EditText) findViewById(R.id.nameValue);
		password=(EditText) findViewById(R.id.passwordValue);
		age=(EditText) findViewById(R.id.ageValue);
		sex=(RadioGroup) findViewById(R.id.sexMenu);
		pingpang=(CheckBox) findViewById(R.id.pingpang);
		football=(CheckBox) findViewById(R.id.football);
		badminton=(CheckBox) findViewById(R.id.badminton);
		basketball=(CheckBox) findViewById(R.id.basketball);
		city=(Spinner) findViewById(R.id.cityValue);
		regist=(Button) findViewById(R.id.regist);
		
		favorites.add(pingpang);
		favorites.add(football);
		favorites.add(badminton);
		favorites.add(basketball);
		
		//创建数组型适配器
		ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,citis);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		city.setAdapter(adapter);
		
		//为注册按钮设置点击监听
		regist.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v){
				
				flag=checkUser();
				if(flag){
					//创建AlertDialog对话框显示要确认的注册信息
					new AlertDialog.Builder(MainActivity.this).setTitle("请确认注册信息:").setMessage("用户名:"+name.getText().toString()+"\n"
							+"密码:"+password.getText().toString()+"\n"
							+"年龄:"+age.getText().toString()+"\n"
							+"性别:"+getSex()+"\n"
							+"爱好:"+getFavorite()+"\n"
							+"城市:"+getCity()+"\n"
							).setCancelable(false).setPositiveButton("确定",new DialogInterface.OnClickListener(){
								public void onClick(DialogInterface dialog,int id){
									progressDialog=ProgressDialog.show(MainActivity.this, "用户信息注册中", "请等待...", true, false);
									
									//创建一个新线程
									new Thread(){  										  
					                    @Override  
					                    public void run() {  
					                        //需要花时间计算的方法  
					                        Calculation.calculate(4);  					                          
					                        //向handler发消息  
					                        handler.sendEmptyMessage(0);  
					                    }}.start();
								}
							}).setNegativeButton("取消",new DialogInterface.OnClickListener(){
								public void onClick(DialogInterface dialog,int id){
									dialog.cancel();
								}
							}).show();
				}
			}
		});				
	}

	private Handler handler = new Handler(){  
		  
        @SuppressLint("HandlerLeak")
		@Override  
        public void handleMessage(Message msg) {               
            //关闭ProgressDialog  
            progressDialog.dismiss();
            //利用Toast提示注册成功
            Toast.makeText(MainActivity.this, "注册成功!", Toast.LENGTH_SHORT).show();     
        }};  
    
    //获取城市信息
	protected String getCity() {
		return citis[city.getSelectedItemPosition()];
	}

	//获取爱好信息
	protected String getFavorite() {
		String favorite="";
		for(CheckBox cb:favorites){
			if(cb.isChecked()){
				favorite+=cb.getText().toString();
				favorite+=",";
			}			
		}
		if(favorite!=""){
			favorite.subSequence(0, favorite.length()-1);
		}else{
			//如果没有勾选爱好,则返回该提示。
			favorite="您未选择爱好!";
		}
		return favorite;
	}

	//获取性别信息
	protected String getSex() {
		RadioButton sexChecked=(RadioButton) findViewById(sex.getCheckedRadioButtonId());
		return sexChecked.getText().toString();
	}

	//检验用户名,密码,年龄是否为空,如果有空的则提示不能为空,并返回false
	protected boolean checkUser() {
		if(name.getText().toString().length()==0){
			name.setError("用户名不能为空!");
			return false;
		}
		if(password.getText().toString().length()==0){
			password.setError("密码不能为空!");
			return false;
		}
		if(age.getText().toString().length()==0){
			age.setError("年龄不能为空!");
			return false;
		}
		return true;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}

Calculation.java
package com.example.housailei;

public class Calculation {
	public static void calculate(int sleepSeconds){  
        try {  
            Thread.sleep(sleepSeconds * 1000);  
        } catch (Exception e) {  
            // TODO: handle exception  
        }  
    }  
}



  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android SDK(Software Development Kit)是一个软件开发工具包,用于开发Android应用程序。它包括开发Android应用程序所需的工具、文档和示例代码。Android SDK提供了一些API,可以让开发者利用Android的特性来创建、编译、测试和调试Android应用程序。它提供了开发者所需的环境和工具来创建Android应用程序,包括Android Studio集成开发环境、Java开发工具和Android设备模拟器等。 Android SDK包括以下主要组件: 1. Android Studio:Android应用程序开发的官方IDE,集成了各种工具和资源,用于创建、编译和测试Android应用程序。 2. Android SDK Tools:Android开发工具,包括命令行工具和其他实用程序,用于构建和测试Android应用程序。 3. Android SDK Platform-tools:ADB(Android Debug Bridge)和其他命令行工具,用于在开发计算机上与Android设备进行通信。 4. Android SDK Build-tools:用于编译、打包和签名Android应用程序的工具。 5. Android SDK Platform:Android操作系统的API级别,包括Android框架和系统库。 6. Android系统镜像:用于在模拟器上运行Android应用程序的虚拟设备系统镜像。 7. Android支持库:提供了一些常用Android应用程序组件和UI控件,以帮助开发者更快地创建应用程序。 总之,Android SDK是开发Android应用程序所必需的工具集,它提供了一套完整的开发环境和工具,让开发者可以更轻松地创建高质量的Android应用程序

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值