文章目錄
- 案例演示--注冊界面
- 實作步驟
- 1.創建新專案
- 2.添加背景圖片
- 3.基于模板創建登錄視窗
- 4.撰寫activity_registration.xml檔案
- 5.撰寫activity_main.xml檔案
- 6.撰寫專案清單檔案
- 7.字串檔案strings.xml
- 8.注冊視窗
- 9.主界面檔案
- 10.運行程式,查看結果
- 輸入資訊時
- 輸入完成后
案例演示–注冊界面
為更好地完善昨天的學習內容,我將昨天所撰寫的代碼進行了修改,完成了注冊界面的Activity事件,
實作步驟
1.創建新專案


2.添加背景圖片

3.基于模板創建登錄視窗


4.撰寫activity_registration.xml檔案

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:gravity="center"
android:padding="15dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvUserLogin"
android:layout_marginBottom="30dp"
android:text="@string/user_registration"
android:textColor="@color/white"
android:textSize="25sp"/>
<!--姓名-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvName"
android:text="@string/user_name"
android:textColor="#000000"
android:textSize="20sp"/>
<EditText
android:layout_width="150dp"
android:layout_height="wrap_content"
android:id="@+id/edtName"
android:ems="10"
android:hint="@string/input_name"
android:singleLine="true"/>
</LinearLayout>
<!--性別-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvSex"
android:text="@string/sex"
android:textColor="#000000"
android:textSize="20sp"/>
<EditText
android:layout_width="150dp"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/input_sex"
android:id="@+id/edtSex"
android:singleLine="true"/>
</LinearLayout>
<!--年齡-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvAge"
android:text="@string/age"
android:textColor="#000000"
android:textSize="20sp"/>
<EditText
android:layout_width="150dp"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/input_age"
android:id="@+id/edtAge"
android:singleLine="true"/>
</LinearLayout>
<!--電話-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvNumber"
android:text="@string/number"
android:textColor="#000000"
android:textSize="20sp"/>
<EditText
android:layout_width="150dp"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/input_number"
android:id="@+id/edtNumber"
android:singleLine="true"/>
</LinearLayout>
<!--郵箱-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvEmail"
android:text="@string/email"
android:textColor="#000000"
android:textSize="20sp"/>
<EditText
android:layout_width="150dp"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/input_email"
android:id="@+id/edtEmail"
android:singleLine="true"/>
</LinearLayout>
<!--主頁-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvMainpage"
android:text="@string/mainpage"
android:textColor="#000000"
android:textSize="20sp"/>
<EditText
android:layout_width="150dp"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/input_mainpage"
android:id="@+id/edtMainpage"
android:singleLine="true"/>
</LinearLayout>
<!--備注-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvRemark"
android:text="@string/remark"
android:textColor="#000000"
android:textSize="20sp"/>
<EditText
android:layout_width="150dp"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/input_remark"
android:id="@+id/edtRemark"
android:singleLine="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnRegistration"
android:paddingRight="30dp"
android:paddingLeft="30dp"
android:text="@string/registration"
android:textSize="20sp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnCancel"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:text="@string/cancel"
android:textSize="20sp"/>
</LinearLayout>
</LinearLayout>
5.撰寫activity_main.xml檔案

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvMessage"
android:textSize="25dp"
android:textColor="#e066ff"/>
</LinearLayout>
6.撰寫專案清單檔案

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.nell.userregistration">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.UserRegistration">
<activity android:name=".RegistrationActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity">
<!--<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>-->
</activity>
</application>
</manifest>
7.字串檔案strings.xml

<resources>
<string name="app_name">UserRegistration</string>
<string name="user_registration">用戶登錄</string>
<string name="user_name">姓名:</string>
<string name="input_name">請輸入您的姓名</string>
<string name="sex">性別:</string>
<string name="input_sex">請輸入您的性別</string>
<string name="age">年齡:</string>
<string name="input_age">請輸入您的年齡</string>
<string name="number">電話:</string>
<string name="input_number">請輸入您的電話</string>
<string name="email">郵箱:</string>
<string name="input_email">請輸入您的郵箱</string>
<string name="mainpage">主頁:</string>
<string name="input_mainpage">請輸入你的主頁</string>
<string name="remark">備注:</string>
<string name="input_remark">請輸入您的備注</string>
<string name="registration">注冊</string>
<string name="cancel">取消</string>
</resources>
8.注冊視窗

package net.nell.userregistration;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
public class RegistrationActivity extends AppCompatActivity {
private EditText edtName;
private EditText edtSex;
private EditText edtAge;
private EditText edtNumber;
private EditText edtEmail;
private EditText edtMainpage;
private EditText edtRemark;
private Button btnRegistration;
private Button btnCancel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registration);
edtName = findViewById(R.id.edtName);
edtSex = findViewById(R.id.edtSex);
edtAge = findViewById(R.id.edtAge);
edtNumber = findViewById(R.id.edtNumber);
edtEmail = findViewById(R.id.edtEmail);
edtMainpage = findViewById(R.id.edtMainpage);
edtRemark = findViewById(R.id.edtRemark);
btnRegistration = findViewById(R.id.btnRegistration);
btnCancel = findViewById(R.id.btnCancel);
btnRegistration.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String strName = edtName.getText().toString().trim();
String strSex = edtSex.getText().toString().trim();
String strAge = edtAge.getText().toString().trim();
String strNumber = edtNumber.getText().toString().trim();
String strEmail = edtEmail.getText().toString().trim();
String strMainpage = edtMainpage.getText().toString().trim();
String strRemark = edtRemark.getText().toString().trim();
Intent intent = new Intent(RegistrationActivity.this,MainActivity.class);
Bundle data = new Bundle();
data.putString("strName:",strName);
data.putString("strSex",strSex);
data.putString("strAge",strAge);
data.putString("strNumber",strNumber);
data.putString("strEmail",strEmail);
data.putString("strMainpage",strMainpage);
data.putString("strRemark",strRemark);
intent.putExtras(data);
startActivity(intent);
}
});
btnCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
9.主界面檔案

package net.nell.userregistration;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private TextView tvMessage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvMessage = findViewById(R.id.tvMessage);
Intent intent = getIntent();
Bundle data = intent.getExtras();
String strName = data.getString("strName");
String strSex = data.getString("strSex");
String strAge = data.getString("strAge");
String strNumber = data.getString("strNumber");
String strEmail = data.getString("strEmail");
String strMainpage = data.getString("strMainpage");
String strRemark = data.getString("strRemark");
String Message = "恭喜,注冊成功!\n 姓名:"+ strName + "\n性別:"+strSex+"\n年齡:"+strAge+"\n電話:"+strNumber+"\n郵箱:"+strEmail+"\n主頁:"+strMainpage+"\n備注:"+strRemark;
tvMessage.setText(Message);
}
}
10.運行程式,查看結果
輸入資訊時

輸入完成后

第二次人物的完成的大概就是如昨天和今天的內容一樣,在玩成任務的程序中,進行學習會使我們的學習內容更加有趣,也可以讓我們更加能夠適應新東西,并且把學習帶的東西,用在實處,明天我們將進行第三個人物的開始,
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/249511.html
標籤:其他
上一篇:問題記錄誤以為自定義view呼叫invalidate()無效,onDraw()方法不重繪
下一篇:資料取證技術面試題(持續更新中)
