利用共享参数实现用户注册
1.创建Register
2. 编辑对应生成的register.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: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>
界面效果
3.编写Strings.xml 资源文件
4.编写activity_main.xml布局资源文件
5.编写Register
package net.lj.hwork27;
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;
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);
}
});
}
}
6.编写MainActivity
package net.lj.hwork27;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
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);
}
}
运行结果如下: