自己动手做的第一个demo,简单的注册页面的实现,并且注册成功后返回注册信息,适用于android新手基本控件的使用。

注册页面的实现:
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.ToggleButton;

public class RegisterActivity extends Activity {
    private static final String places[] = {"中国","中国香港","中国台湾","中国澳门"};
    private boolean isNotified = false;
    private int sexFlag = 0;
    private boolean isChecked = false;
    private int plcFlag = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
//设置标题
setTitle("注册成功");
//获取控件
final EditText username = (EditText)findViewById(R.id.username);
final EditText pwd = (EditText)findViewById(R.id.pwd);
final EditText pwdrp = (EditText)findViewById(R.id.pwdrp);
RadioGroup sex = (RadioGroup)findViewById(R.id.sex);
Spinner from = (Spinner)findViewById(R.id.from);
final EditText mail= (EditText)findViewById(R.id.mail);
ToggleButton notify = (ToggleButton)findViewById(R.id.notify);
CheckBox check = (CheckBox)findViewById(R.id.check);
Button register = (Button)findViewById(R.id.register);
Button cancel = (Button)findViewById(R.id.cancel);
//给Spinner设置适配器

ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item,places);
//设置下拉菜单样式
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
from.setAdapter(adapter);
//事件监听
notify.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton arg0, boolean flag) {
// TODO Auto-generated method stub
isNotified = flag;
}
});
check.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton arg0, boolean flag) {
// TODO Auto-generated method stub
isChecked = flag;
}
});
sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(RadioGroup arg0, int flag) {
// TODO Auto-generated method stub
sexFlag = flag;
}
});
from.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0,View arg1,int flag,long arg3){
plcFlag = flag;    
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}
});
register.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String strUser = username.getText().toString();
String strPwd = pwd.getText().toString();
String strPwdrp = pwdrp.getText().toString();
String strMail = mail.getText().toString();
if(strUser.equals("")){
//弹出对话框
new AlertDialog.Builder(RegisterActivity.this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("警告")
.setMessage("请输入用户名")
.setPositiveButton("确定", null)
.show();
return;
}
if(!strPwd.equals(strPwdrp)){
//弹出对话框
new AlertDialog.Builder(RegisterActivity.this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("警告")
.setMessage("两次输入的密码不相同")
.setPositiveButton("确定", null)
.show();
return;
}
if(!isChecked){
//弹出对话框
new AlertDialog.Builder(RegisterActivity.this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("警告")
.setMessage("请勾选同意条款")
.setPositiveButton("确定", null)
.show();
return;
}
//发送数据到另一个Activity
Bundle bundle = new Bundle();
bundle.putString("username", strUser);
bundle.putString("mail", strMail);
bundle.putString("from", places[plcFlag]);
//bundle.putString("from", "中国");
bundle.putBoolean("notify", isNotified);
bundle.putBoolean("check", isChecked);
bundle.putInt("sex", sexFlag);
Intent intent = new Intent(RegisterActivity.this,ResultActivity.class);
intent.putExtra("info", bundle);
//启动这个Activity
RegisterActivity.this.startActivity(intent);
//结束本Activity
RegisterActivity.this.finish();
}
});
cancel.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
RegisterActivity.this.finish();
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.register, menu);
return true;
}
}

注册结果显示activity:

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class ResultActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.registerresult);
//设置标题
setTitle("注册成功");
//接收数据
Intent intent = this .getIntent();
Bundle bundle = intent.getBundleExtra("info");
String strUsername = bundle.getString("username");
String strMail = bundle.getString("mail");
String strFrom = bundle.getString("from");
String strNotify = bundle.getBoolean("notify") ? "是":"否";;
String strSex = bundle.getInt("sexFlag") == 0 ? "男":"女";;

TextView username = (TextView)findViewById(R.id.username);
username.setText(strUsername);
TextView mail = (TextView)findViewById(R.id.mail);
mail.setText(strMail);
TextView from = (TextView)findViewById(R.id.from);
from.setText(strFrom);
TextView notify = (TextView)findViewById(R.id.notify);
notify.setText(strNotify);
TextView sex = (TextView)findViewById(R.id.sex);
sex.setText(strSex);

Button doneBt = (Button)findViewById(R.id.done);
doneBt.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
ResultActivity.this.finish();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.register, menu);
return true;
}
}

XML文件:

<?xml version="1.0" encoding="utf-8"?> 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:layout_gravity="center"> 
            android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:gravity="center">   
                    android:layout_height="wrap_content"
            android:gravity="center">
                            android:layout_width="fill_parent" 
                android:layout_height="wrap_content"
                android:text="用户名"/>
                            android:layout_width="fill_parent" 
                android:layout_height="wrap_content"
                android:id="@+id/username"/>
       
 
                    android:layout_height="wrap_content"
            android:gravity="center">
                            android:layout_width="wrap_content" 
                android:layout_height="wrap_content"
                android:text="密码"/>
                            android:layout_width="wrap_content" 
                android:layout_height="wrap_content"
                android:id="@+id/pwd"
                android:inputType="textPassword"/>
       
 
                    android:layout_height="wrap_content"
            android:gravity="center">
                            android:layout_width="wrap_content" 
                android:layout_height="wrap_content"
                android:text="确认密码"/>
                            android:layout_width="wrap_content" 
                android:layout_height="wrap_content"
                android:id="@+id/pwdrp"
                android:inputType="textPassword"/>
       
 
                    android:layout_height="wrap_content"
            android:gravity="center">
                            android:layout_width="wrap_content" 
                android:layout_height="wrap_content"
                android:text="性别"/>
                   android:id="@+id/sex"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:orientation="horizontal">
                   android:id="@+id/male"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:checked="true"
       android:text="男"/>
                       android:layout_width="wrap_content" 
               android:layout_height="wrap_content"
               android:text=" "/>
                   android:id="@+id/famale"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:checked="true"
       android:text="女"/>
     
       
   
                    android:layout_height="wrap_content"
            android:gravity="center">
                            android:layout_width="wrap_content" 
                android:layout_height="wrap_content"
                android:text="来自"/>
                            android:id="@+id/from"
android:layout_width="80dp"
android:layout_height="50dp"/>
       
 
                    android:layout_height="wrap_content"
            android:gravity="center">
                            android:layout_width="wrap_content" 
                android:layout_height="wrap_content"
                android:text="邮箱地址"/>
                            android:layout_width="wrap_content" 
                android:layout_height="wrap_content"
                android:id="@+id/mail"
                android:inputType="textEmailAddress"/>
       
 
                    android:layout_height="wrap_content"
            android:gravity="center">
                            android:layout_width="wrap_content" 
                android:layout_height="wrap_content"
                android:text="邮件通知"/>
                            android:layout_width="wrap_content" 
                android:layout_height="wrap_content">
                        android:id="@+id/notify"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="toggleButton"/>  
           
       
 
                    android:layout_height="wrap_content"
            android:gravity="center">
                            android:id="@+id/check"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="同意条款"/>
       
 
     
       android:orientation="horizontal" 
   android:layout_width="fill_parent" 
   android:layout_height="wrap_content"
   android:gravity="center">