主界面
主布局资源文件activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="#b5e9d2"
android:padding="15dp"
tools:context="com.example.registration.RegistrationActivity">
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#0000ff"
android:textSize="30sp"
android:layout_marginTop="20dp"
android:layout_marginBottom="15dp"
android:text="@string/title"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:id="@+id/tv_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="20sp"
android:layout_marginBottom="15dp"
android:text="@string/username"
android:layout_below="@+id/tv_title"
android:layout_alignParentStart="true" />
<EditText
android:id="@+id/edt_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/tv_username"
android:layout_alignBaseline="@+id/tv_username"
android:singleLine="true"
android:hint="@string/input_username"/>
<TextView
android:id="@+id/tv_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="20sp"
android:layout_marginBottom="15dp"
android:layout_below="@+id/tv_username"
android:text="@string/password" />
<EditText
android:id="@+id/edt_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/tv_password"
android:layout_alignBaseline="@+id/tv_password"
android:layout_alignLeft="@+id/edt_username"
android:singleLine="true"
android:hint="@string/input_password"
android:inputType="textPassword"/>
<TextView
android:id="@+id/tv_phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="20sp"
android:layout_marginBottom="15dp"
android:layout_below="@+id/tv_password"
android:text="@string/phone" />
<EditText
android:id="@+id/edt_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/tv_phone"
android:layout_alignBaseline="@+id/tv_phone"
android:layout_alignLeft="@+id/edt_password"
android:singleLine="true"
android:hint="@string/input_phone"
android:inputType="phone"/>
<TextView
android:id="@+id/tv_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="20sp"
android:layout_marginBottom="15dp"
android:layout_below="@+id/tv_phone"
android:text="@string/email" />
<EditText
android:id="@+id/edt_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/tv_email"
android:layout_alignBaseline="@+id/tv_email"
android:layout_alignLeft="@+id/edt_phone"
android:singleLine="true"
android:hint="@string/input_email"
android:inputType="textEmailAddress"/>
<TextView
android:id="@+id/tv_gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/gender"
android:textSize="20sp"
android:layout_marginBottom="15dp"
android:textColor="#000000"
android:layout_below="@id/tv_email"
android:layout_alignParentStart="true" />
<RadioGroup
android:id="@+id/rg_gender"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/tv_gender"
android:layout_below="@id/tv_email"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rb_male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="30dp"
android:text="@string/male"
android:textSize="20sp"/>
<RadioButton
android:id="@+id/rb_female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/female"
android:textSize="20sp"/>
</RadioGroup>
<TextView
android:id="@+id/tv_hobby"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hobby"
android:textSize="20sp"
android:layout_marginBottom="15dp"
android:textColor="#000000"
android:layout_below="@+id/tv_gender"
android:layout_alignParentStart="true" />
<CheckBox
android:id="@+id/cb_travel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/rg_gender"
android:layout_alignBaseline="@id/tv_hobby"
android:textSize="20sp"
android:text="@string/travel"
android:layout_toEndOf="@+id/tv_hobby" />
<CheckBox
android:id="@+id/cb_music"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/cb_travel"
android:layout_alignBaseline="@id/tv_hobby"
android:text="@string/music"
android:textSize="20sp"/>
<CheckBox
android:id="@+id/cb_food"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/cb_music"
android:layout_alignBaseline="@id/tv_hobby"
android:text="@string/food"
android:textSize="20sp"/>
<Button
android:id="@+id/btn_submit"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="@string/submit"
android:textSize="20sp"
android:layout_below="@+id/tv_hobby"
android:onClick="doSubmit"
android:layout_alignParentStart="true" />
<Button
android:id="@+id/btn_reset"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_below="@id/tv_hobby"
android:layout_alignRight="@id/rg_gender"
android:text="@string/reset"
android:onClick="doReset"
android:textSize="20sp"/>
</RelativeLayout>
主界面类MainActivity
package com.example.registration;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
public class RegistrationActivity extends Activity {
private EditText edtUsername;
private EditText edtPassword;
private EditText edtPhone;
private EditText edtEmail;
private RadioGroup rgGender;
private RadioButton rbMale;
private RadioButton rbFemale;
private CheckBox cbTravel;
private CheckBox cbMusic;
private CheckBox cbFood;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registration);
edtUsername = (EditText) findViewById(R.id.edt_username);
edtPassword = (EditText) findViewById(R.id.edt_password);
edtPhone= (EditText) findViewById(R.id.edt_phone);
edtEmail = (EditText) findViewById(R.id.edt_email);
rgGender = (RadioGroup) findViewById(R.id.rg_gender);
rbMale = (RadioButton) findViewById(R.id.rb_male);
rbFemale = (RadioButton) findViewById(R.id.rb_female);
cbTravel = (CheckBox) findViewById(R.id.cb_travel);
cbMusic = (CheckBox) findViewById(R.id.cb_music);
cbFood = (CheckBox) findViewById(R.id.cb_food);
}
public void doSubmit(View view){
//获取用户名
String strUsername = edtUsername.getText().toString().trim();
//获取密码
String strPassword = edtPassword.getText().toString().trim();
//获取电话
String strPhone = edtPhone.getText().toString().trim();
//获取邮箱
String strEmail = edtEmail.getText().toString().trim();
//获取性别
String strGender = "";
switch (rgGender.getCheckedRadioButtonId()){
case R.id.rb_male:
strGender = rbMale.getText().toString();
break;
case R.id.rb_female:
strGender = rbFemale.getText().toString();
break;
}
//获取爱好
StringBuilder builder = new StringBuilder();
if (cbTravel.isChecked()){
builder.append(cbTravel.getText().toString() + " ");
}
if (cbMusic.isChecked()){
builder.append(cbMusic.getText().toString() + " ");
}
if (cbFood.isChecked()){
builder.append(cbFood.getText().toString());
}
String strHobby = builder.toString().trim();
//创建数据包
Bundle data = new Bundle();
data.putString("username",strUsername);
data.putString("password",strPassword);
data.putString("phone",strPhone);
data.putString("email",strEmail);
data.putString("gender",strGender);
data.putString("hobby",strHobby);
//创建意图,跳转窗口,传递数据
Intent intent = new Intent(RegistrationActivity.this,InformationActivity.class);
intent.putExtras(data);
startActivity(intent);
}
public void doReset(View view){
edtUsername.setText("");
edtPassword.setText("");
edtPhone.setText("");
edtEmail.setText("");
rbMale.setChecked(true);
cbTravel.setChecked(false);
cbMusic.setChecked(false);
cbFood.setChecked(false);
edtUsername.requestFocus();
}
}
跳转界面
主布局资源文件activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#6ffcb6"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="27dp"
android:text="@string/display_info"
android:textColor="#0000ff"
android:textSize="30sp" />
<TextView
android:id="@+id/tv_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/tv_info"
android:layout_marginLeft="30dp"
android:layout_marginTop="40dp"
android:text="@string/username"
android:textSize="20sp" />
<TextView
android:id="@+id/tv_username_info"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/tv_username"
android:layout_marginLeft="16dp"
android:layout_toRightOf="@+id/tv_username"
android:text="@string/username"
android:textColor="#ff00ff"
android:textSize="20sp" />
<TextView
android:id="@+id/tv_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/tv_username"
android:layout_marginLeft="30dp"
android:layout_marginTop="20dp"
android:text="@string/password"
android:textSize="20sp" />
<TextView
android:id="@+id/tv_password_info"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tv_username_info"
android:layout_alignTop="@+id/tv_password"
android:layout_below="@+id/tv_username_info"
android:text="@string/password"
android:textColor="#ff00ff"
android:textSize="20sp" />
<TextView
android:id="@+id/tv_phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/tv_password"
android:layout_marginLeft="30dp"
android:layout_marginTop="20dp"
android:text="@string/phone"
android:textSize="20sp" />
<TextView
android:id="@+id/tv_phone_info"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tv_password_info"
android:layout_alignTop="@+id/tv_phone"
android:layout_below="@+id/tv_password_info"
android:text="@string/phone"
android:textColor="#ff00ff"
android:textSize="20sp" />
<TextView
android:id="@+id/tv_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/tv_phone"
android:layout_marginLeft="30dp"
android:layout_marginTop="20dp"
android:text="@string/email"
android:textSize="20sp" />
<TextView
android:id="@+id/tv_email_info"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tv_phone_info"
android:layout_alignTop="@+id/tv_email"
android:layout_below="@+id/tv_phone_info"
android:text="@string/email"
android:textColor="#ff00ff"
android:textSize="20sp" />
<TextView
android:id="@+id/tv_gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/tv_email"
android:layout_marginLeft="30dp"
android:layout_marginTop="20dp"
android:text="@string/gender"
android:textSize="20sp" />
<TextView
android:id="@+id/tv_gender_info"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tv_email_info"
android:layout_alignTop="@+id/tv_gender"
android:layout_below="@+id/tv_email_info"
android:text="@string/gender"
android:textColor="#ff00ff"
android:textSize="20sp" />
<TextView
android:id="@+id/tv_hobby"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/tv_gender"
android:layout_marginLeft="30dp"
android:layout_marginTop="20dp"
android:text="@string/hobby"
android:textSize="20sp" />
<TextView
android:id="@+id/tv_hobby_info"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tv_gender_info"
android:layout_alignTop="@+id/tv_hobby"
android:layout_below="@+id/tv_gender_info"
android:text="@string/hobby"
android:textColor="#ff00ff"
android:textSize="20sp" />
</RelativeLayout>
主界面类MainActivity
package com.example.registration;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class InformationActivity extends Activity {
private TextView tvUsernameInfo;
private TextView tvPasswordInfo;
private TextView tvPhoneInfo;
private TextView tvEmailInfo;
private TextView tvGenderInfo;
private TextView tvHobbyInfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_information);
//通过资源标识获取控件实例
tvUsernameInfo = (TextView)findViewById(R.id.tv_username_info);
tvPasswordInfo = (TextView)findViewById(R.id.tv_password_info);
tvPhoneInfo = (TextView)findViewById(R.id.tv_phone_info);
tvEmailInfo = (TextView)findViewById(R.id.tv_email_info);
tvGenderInfo= (TextView)findViewById(R.id.tv_gender_info);
tvHobbyInfo = (TextView)findViewById(R.id.tv_hobby_info);
//获取意图传递的数据
Bundle data = getIntent().getExtras();
String strUsername = data.getString("username");
String strPassword = data.getString("password");
String strPhone = data.getString("phone");
String strEmail = data.getString("email");
String strGender = data.getString("gender");
String strHobby = data.getString("hobby");
//利用获取的数据设置标签内容
tvUsernameInfo.setText(strUsername);
tvPasswordInfo.setText(strPassword);
tvPhoneInfo.setText(strPhone);
tvEmailInfo.setText(strEmail);
tvGenderInfo.setText(strGender);
tvHobbyInfo.setText(strHobby);
}
}