为更好地完善昨天的学习内容,我将昨天所编写的代码进行了修改,完成了注册界面的Activity事件。
<?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>
<?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>
<?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>
<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>
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();
}
});
}
}
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);
}
}
第二次人物的完成的大概就是如昨天和今天的内容一样。在玩成任务的过程中,进行学习会使我们的学习内容更加有趣,也可以让我们更加能够适应新东西,并且把学习带的东西,用在实处。明天我们将进行第三个人物的开始。