1、创建安卓应用
2、基于面板创建一个Register类
![在这里插入图片描述](https://img-blog.csdnimg.cn/20201227174554319.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl81MDY1OTUzNA==,size_16,color_FFFFFF,t_70)
3、编写activity_register界面
<?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:gravity="center"
android:padding="15dp"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:text="@string/userRegister"
android:textColor="#ff00ff"
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:text="@string/userName"
android:textColor="#000000"
android:textSize="20sp" />
<EditText
android:id="@+id/edtName"
android:layout_width="150dp"
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_horizontal"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sex"
android:textColor="#000000"
android:textSize="20sp" />
<EditText
android:id="@+id/edtSex"
android:layout_width="150dp"
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_horizontal"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/age"
android:textColor="#000000"
android:textSize="20sp" />
<EditText
android:id="@+id/edtAge"
android:layout_width="150dp"
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_horizontal"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hobby"
android:textColor="#000000"
android:textSize="20sp" />
<EditText
android:id="@+id/edtHobby"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:ems="10"
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:id="@+id/btnRegister"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:text="@string/btnRegister"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
4、编写strings资源文件
<resources>
<string name="app_name">作业27</string>
<string name="userRegister">用户注册</string>
<string name="userName">姓名:</string>
<string name="sex">性别:</string>
<string name="age">年龄:</string>
<string name="hobby">爱好:</string>
<string name="btnRegister">注册</string>
</resources>
5、编写activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:gravity="center"
tools:context=".MainActivity">
<TextView
android:id="@+id/tvMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"
android:textColor="#0000ff"/>
</LinearLayout>
6、编写Register类
package net.yc.homework27;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import net.yc.homework27.MainActivity;
import net.yc.homework27.R;
public class Register extends AppCompatActivity {
private EditText edtName;
private EditText edtSex;
private EditText edtAge;
private EditText edtHobby;
private Button btnRegister;
private SharedPreferences sharedPreferences;
private static final String NAME = "user_info";
private static final int MODE = Context.MODE_PRIVATE;
private SharedPreferences.Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
edtName = findViewById(R.id.edtName);
edtSex = findViewById(R.id.edtSex);
edtAge = findViewById(R.id.edtAge);
edtHobby = findViewById(R.id.edtHobby);
btnRegister = findViewById(R.id.btnRegister);
sharedPreferences = getSharedPreferences(NAME,MODE);
editor = sharedPreferences.edit();
btnRegister.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 strHobby = edtHobby.getText().toString().trim();
editor.putString("strName",strName);
editor.putString("strSex",strSex);
editor.putString("strAge",strAge);
editor.putString("strHobby",strHobby);
if (editor.commit()) {
Toast.makeText(Register.this,"恭喜,注册成功",Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(Register.this,"遗憾,注册失败",Toast.LENGTH_SHORT).show();
}
Intent intent = new Intent(Register.this, MainActivity.class);
startActivity(intent);
}
});
}
}
7、编写 MainActicity 类
`package net.yc.homework27;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import net.yc.homework27.R;
public class MainActivity extends AppCompatActivity {
private TextView tvMessage;
private SharedPreferences sharedPreferences;
private static final String NAME = "user_info";
private static final int MODE = Context.MODE_PRIVATE;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvMessage = findViewById(R.id.tvMessage);
sharedPreferences = getSharedPreferences(NAME,MODE);
String strName = sharedPreferences.getString("strName","");
String strAge = sharedPreferences.getString("strAge","");
String strSex = sharedPreferences.getString("strSex","");
String strHobby = sharedPreferences.getString("strHobby","");
StringBuilder builder = new StringBuilder();
builder.append("姓名:" + strName + "\n");
builder.append("性别:" + strSex + "\n");
builder.append("年龄:" + strAge + "\n");
builder.append("爱好" + strHobby + "\n");
String personInfo = builder.toString();
tvMessage.setText(personInfo);
}
}
8、启动应用查看效果