目录
基于Empty Activity模板创建安卓应用 - UserRegistration
注册界面布局资源文件activity_registration.xml输入代码
打开 信息界面布局资源文件 - activity_information.xml输入代码
打开用户注册界面类 - RegistrationActivity输入代码
打开注册信息显示界面 - InformationActivity输入代码
编辑框常用属性
属性 | 含义 |
---|---|
text | 文本内容 |
textSize | 文本字号,单位:sp |
textColor | 文本颜色,#ff0000 - 红色 |
hint | 提示信息 |
singleLine | 单行(true or false) |
layout_height | 高度,单位:dp (wrap_content, match_parent) |
layout_weight | 宽度,单位:dp (wrap_content, match_parent) |
inputType | 输入类型(普通文本、密码、邮件……) |
maxLines | 最大行数 |
lines | 行数 |
具体例子
最后效果
基于Empty Activity
模板创建安卓应用 - UserRegistration
准备图片素材
将两张背景图片拷贝到drawable
目录
主界面与主布局资源文件更名并新建信息界面
选中MainActivity>Refactor>Rename
右击net.zvt.userregistration>New>Java Class
打开字符串资源文件strings.xml
具体代码:
<resources> <string name="app_name">用户注册</string> <string name="name">姓名:</string> <string name="gender">性别:</string> <string name="age">年龄:</string> <string name="phone">电话:</string> <string name="email">邮箱:</string> <string name="home_page">主页:</string> <string name="memo">备注:</string> <string name="register">注册</string> <string name="cancel">取消</string> </resources>
注册界面布局资源文件activity_registration.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:background="@drawable/reg_bg" android:gravity="center" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center"> <TextView android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/name" android:textColor="#000000" android:textSize="16sp" /> <EditText android:id="@+id/et_name" android:layout_width="200dp" android:layout_height="wrap_content" android:ems="10" android:singleLine="true" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center"> <TextView android:id="@+id/tv_gender" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/gender" android:textColor="#000000" android:textSize="16sp" /> <EditText android:id="@+id/et_gender" android:layout_width="200dp" android:layout_height="wrap_content" android:ems="10" android:singleLine="true" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center"> <TextView android:id="@+id/tv_age" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/age" android:textColor="#000000" android:textSize="16sp" /> <EditText android:id="@+id/et_age" android:layout_width="200dp" android:layout_height="wrap_content" android:ems="10" android:inputType="number" android:singleLine="true" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center"> <TextView android:id="@+id/tv_phone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/phone" android:textColor="#000000" android:textSize="16sp" /> <EditText android:id="@+id/et_phone" android:layout_width="200dp" android:layout_height="wrap_content" android:ems="10" android:inputType="phone" android:singleLine="true" /> <!-- ems的意思是一行占据的空间=10个M占据的空间。也因此,width就要设置为wrap-content--> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center"> <TextView android:id="@+id/tv_email" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/email" android:textColor="#000000" android:textSize="16sp" /> <EditText android:id="@+id/et_email" android:layout_width="200dp" android:layout_height="wrap_content" android:ems="10" android:inputType="textEmailAddress" android:singleLine="true" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center"> <TextView android:id="@+id/tv_homepage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/home_page" android:textColor="#000000" android:textSize="16sp" /> <EditText android:id="@+id/et_homepage" android:layout_width="200dp" android:layout_height="wrap_content" android:ems="10" android:inputType="textUri" android:singleLine="true" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center"> <TextView android:id="@+id/tv_memo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/memo" android:textColor="#000000" android:textSize="16sp" /> <EditText android:id="@+id/et_memo" android:layout_width="200dp" android:layout_height="wrap_content" android:ems="10" android:lines="4" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center"> <Button android:id="@+id/btn_register" android:layout_width="80dp" android:layout_height="wrap_content" android:onClick="doRegister" android:layout_marginRight="15dp" android:text="@string/register" /> <Button android:id="@+id/btn_cancel" android:layout_width="80dp" android:layout_height="wrap_content" android:onClick="doCancel" android:text="@string/cancel" /> </LinearLayout> </LinearLayout>
查看预览效果
打开 信息界面布局资源文件 - activity_information.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:background="@drawable/info_bg" android:orientation="vertical" android:padding="20dp"> <TextView android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:textColor="#0000ff" android:textSize="18sp" /> <TextView android:id="@+id/tv_gender" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:textColor="#0000ff" android:textSize="18sp" /> <TextView android:id="@+id/tv_age" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:textColor="#0000ff" android:textSize="18sp" /> <TextView android:id="@+id/tv_phone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:autoLink="phone" android:textColor="#0000ff" android:textSize="18sp" /> <TextView android:id="@+id/tv_email" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:autoLink="email" android:textColor="#0000ff" android:textSize="18sp" /> <TextView android:id="@+id/tv_homepage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:autoLink="web" android:textColor="#0000ff" android:textSize="18sp" /> <TextView android:id="@+id/tv_memo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:textColor="#0000ff" android:textSize="18sp" /> </LinearLayout>
查看预览效果

打开用户注册界面类 - RegistrationActivity输入代码
具体代码
package net.zyt.userregistration; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.EditText; public class RegistrationActivity extends AppCompatActivity { //声明变量 private EditText etName; private EditText etGender; private EditText etAge; private EditText etPhone; private EditText etEmail; private EditText etHomepage; private EditText etMemo; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //利用布局资源文件设置用户界面 setContentView(R.layout.activity_registration); //通过资源标识符获取控件 etName=findViewById(R.id.et_name); etGender=findViewById(R.id.et_gender); etAge=findViewById(R.id.et_age); etPhone=findViewById(R.id.et_phone); etEmail=findViewById(R.id.et_email); etHomepage=findViewById(R.id.et_homepage); etMemo=findViewById(R.id.et_memo); } /** * 注册按钮单击事件处理方法 * // @param view */ // 注册按钮单击事件处理方法 public void doRegister(View view){ //获取用户输入数据 String name = etName.getText().toString(); String gender = etGender.getText().toString(); String age = etAge.getText().toString(); String phone = etPhone.getText().toString(); String email = etEmail.getText().toString(); String homePage = etHomepage.getText().toString(); String memo = etMemo.getText().toString(); //将各项数据打包 Bundle data = new Bundle(); data.putString("name", name); data.putString("gender", gender); data.putString("age", age); data.putString("phone", phone); data.putString("email", email); data.putString("home_page", homePage); data.putString("memo", memo); //创建意图,指定起始组件与目标组件 Intent intent=new Intent(this,informationActivity.class); //利用意图携带数据包 intent.putExtras(data); //按意图启动目标组件 startActivity(intent); } public void doCancel(View view){ finish(); } }
打开注册信息显示界面 - InformationActivity输入代码
具体代码:
package net.zyt.userregistration; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.widget.TextView; public class informationActivity extends AppCompatActivity { private TextView tvName; private TextView tvGender; private TextView tvAge; private TextView tvPhone; private TextView tvEmail; private TextView tvHomePage; private TextView tvMemo; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //利用布局资源文件设置用户界面 setContentView(R.layout.activity_information); // 通过资源标识获得控件示例 tvName = findViewById(R.id.tv_name); tvGender = findViewById(R.id.tv_gender); tvAge = findViewById(R.id.tv_age); tvPhone = findViewById(R.id.tv_phone); tvEmail = findViewById(R.id.tv_email); tvHomePage = findViewById(R.id.tv_homepage); tvMemo = findViewById(R.id.tv_memo); // 获得意图 Intent intent = getIntent(); if (intent != null) { // 获得意图携带的数据包 Bundle data = intent.getExtras(); // 从数据包里按键取值 String name = data.getString("name"); String gender = data.getString("gender"); String age = data.getString("age"); String phone = data.getString("phone"); String email = data.getString("email"); String homepage = data.getString("home_page"); String memo = data.getString("memo"); // 设置各个标签的内容 tvName.setText("姓名:" +name ); tvGender.setText("性别:" + gender); tvAge.setText("年龄:" + age); tvPhone.setText("电话:" + phone); tvEmail.setText("邮箱:" + email); tvHomePage.setText("主页:" + homepage); tvMemo.setText("备注:" + memo); } } }