天天看點

安卓27作業 - 利用共享參數實作使用者注冊利用共享參數實作使用者注冊

利用共享參數實作使用者注冊

1.建立Register

安卓27作業 - 利用共享參數實作使用者注冊利用共享參數實作使用者注冊
安卓27作業 - 利用共享參數實作使用者注冊利用共享參數實作使用者注冊

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>

           

界面如下:

安卓27作業 - 利用共享參數實作使用者注冊利用共享參數實作使用者注冊

3.編寫Strings.xml 資源檔案

安卓27作業 - 利用共享參數實作使用者注冊利用共享參數實作使用者注冊

4.編寫activity_main.xml布局資源檔案

安卓27作業 - 利用共享參數實作使用者注冊利用共享參數實作使用者注冊

5.編寫Register

package net.zxj.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.zxj.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);

    }
}
           

運作結果如下:

安卓27作業 - 利用共享參數實作使用者注冊利用共享參數實作使用者注冊

點選注冊後:

安卓27作業 - 利用共享參數實作使用者注冊利用共享參數實作使用者注冊