1、布局文件
<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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名" />
<EditText
android:id="@+id/user"
android:layout_width="200px"
android:layout_height="30px"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码" />
<EditText
android:id="@+id/pwd"
<span style="white-space:pre"> android:inputType="textPassword"</span>
android:layout_width="200px"
android:layout_height="30px"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确认密码" />
<EditText
android:id="@+id/repwd"
<span style="white-space:pre"> android:inputType="textPassword"</span>
android:layout_width="200px"
android:layout_height="30px"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="邮箱" />
<EditText
android:id="@+id/email"
android:layout_width="200px"
android:layout_height="30px"/>
<Button
android:id="@+id/submit"
android:text="提交"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Button submit = (Button)findViewById(R.id.submit);
submit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String user = ((EditText)findViewById(R.id.user)).getText().toString();
String pwd = ((EditText)findViewById(R.id.pwd)).getText().toString();
String repwd = ((EditText)findViewById(R.id.repwd)).getText().toString();
String email = ((EditText)findViewById(R.id.email)).getText().toString();
if(!"".equals(user)&&!"".equals(pwd)&&!"".equals(email)){
if(!pwd.equals(repwd)){
Toast.makeText(MainActivity.this, "两次输入的密码不一致,请重新输入", Toast.LENGTH_SHORT).show();
((EditText)findViewById(R.id.pwd)).setText("");//清空密码编辑框
((EditText)findViewById(R.id.repwd)).setText("");//清空确认密码编辑框
((EditText)findViewById(R.id.pwd)).requestFocus();//让密码编辑框获得焦点
}else{
//将输入的信息保存到Bundle中,并启动一个新的Activity显示输入的用户注册信息
Intent intent = new Intent(MainActivity.this, RegisterAcitivity.class);
Bundle bundle = new Bundle();//创建并实例化一个Bundle对象
bundle.putCharSequence("user", user);//保存用户名
bundle.putCharSequence("pwd", pwd);// 保存密码
bundle.putCharSequence("email", email);//保存E-mail地址
intent.putExtras(bundle);//将Bundle对象添加到Intent对象中
startActivity(intent);//启动新的Activity
}
}else{
Toast.makeText(MainActivity.this, "请将注册信息输入完整", Toast.LENGTH_SHORT).show();
}
}
});
3、register.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" >
<TextView
android:id="@+id/user"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/pwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/email"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
4、RegisterActivity.java
public class RegisterActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);//设置该Activity中要显示的内容视图
Intent intent = getIntent();//获取Intent对象
Bundle bundle = intent.getExtras();//获取传递的数据包
TextView user = (TextView)findViewById(R.id.user);
user.setText("用户名:"+bundle.getString("user"));
TextView pwd = (TextView)findViewById(R.id.pwd);
pwd.setText("密码:"+bundle.getString("pwd"));
TextView email = (TextView)findViewById(R.id.email);
pwd.setText("E-mail:"+bundle.getString("email"));
}
}
5、注册RegisterActivity
<activity
android:label="显示用户注册信息"
android:icon="@drawable/ic_launcher"
android:name=".RegisterActivity"></activity>
调用另一个Activity并返回结果
1、定义一个名称为CODE的变量,用于设置requestCode请求码
final int CODE = 0x717;//定义一个请求码变量
2、将startActivity()方法,改为使用startActivityForResult()方法
startActivityForResult(intent, CODE);//启动新的Activity
3、register.xml布局文件添加一个返回按钮
<Button
android:id="@+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="返回上一步"/>
4、为返回按钮添加单击监听事件
Button button = (Button)findViewById(R.id.back);//获取“返回上一步”按钮
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
setResult(0x717, intent);//设置返回的结果码,并返回调用该Activity的Activity
finish();
}
});
5、MainActivity重写onActivityResult()方法
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==CODE&&resultCode==CODE){
((EditText)findViewById(R.id.pwd)).setText("");//清空“密码”编辑框
((EditText)findViewById(R.id.repwd)).setText("");//清空“确认密码”编辑框
}
}