利用共享参数实现用户注册--27讲作业

1、创建安卓应用

2、基于面板创建一个Register类

在这里插入图片描述

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、启动应用查看效果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值