Table of contents
Create an Android application based on the Empty Activity template - UserRegistration
Rename the main interface and main layout resource files and create a new information interface
Open the string resource file strings.xml
Registration interface layout resource file activity_registration.xml input code
Open the information interface layout resource file - activity_information.xml input code
Open the user registration interface class - RegistrationActivity input code
Open the registration information display interface - InformationActivity input code
Edit box common properties
Attributes | meaning |
---|---|
text | text content |
textSize | Text font size, unit: sp |
textColor | Text color, #ff0000 - red |
hint | prompt information |
singleLine | single line (true or false) |
layout_height | Height, unit: dp (wrap_content, match_parent) |
layout_weight | Width, unit: dp (wrap_content, match_parent) |
inputType | Type of input (plain text, password, email...) |
maxLines | Maximum number of lines |
lines | Number of lines |
concrete example
final effect
Create an Android application based on Empty Activity
a template - UserRegistration
Prepare image material
Copy the two background images to drawable
the directory
Rename the main interface and main layout resource files and create a new information interface
Select MainActivity>Refactor>Rename
Right click on net.zvt.userregistration>New>Java Class
Open the string resource file strings.xml
Specific code:
<resources> <string name="app_name">User Registration</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>
Registration interface layout resource file activity_registration.xml input code
Specific code:
<?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 means the space occupied by one line = the space occupied by 10 M. Therefore, the width should be set to 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>
Check out the preview
Open the information interface layout resource file - activity_information.xml输入代码
Specific code:
<?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>
Check out the preview
Open the user registration interface class - RegistrationActivity输入代码
specific code
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 { //declare variables 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); //Use the layout resource file to set up the user interface setContentView(R.layout.activity_registration); / / Get the control through the resource identifier 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); } /** * Register button click event handler method * // @param view */ // Register button click event handler method public void doRegister(View view){ //Get user input data 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(); // pack the data 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); //Create intent, specify starting component and target component Intent intent=new Intent(this,informationActivity.class); //Use the intent to carry the data packet intent.putExtras(data); //Start the target component by intent startActivity(intent); } public void doCancel(View view){ finish(); } }
Open the registration information display interface - InformationActivity输入代码
Specific code:
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); //Use the layout resource file to set up the user interface setContentView(R.layout.activity_information); // Get the control example through the resource ID 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); // get intent Intent intent = getIntent(); if (intent != null) { // Get the data packet carried by the intent Bundle data = intent.getExtras(); // Get the value from the key in the data packet 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"); // Set the content of each label tvName.setText("Name:" +name ); tvGender.setText("Gender: " + gender); tvAge.setText("Age: " + age); tvPhone.setText("Phone: " + phone); tvEmail.setText("Email: " + email); tvHomePage.setText("Home page: " + homepage); tvMemo.setText("Remark: " + memo); } } }