Android開發使用SharedPreferences實現學生基本信息錄入

實現內容

使用SharedPreferences實現學生基本信息錄入。圖1爲主界面,剛進入的時候,讀取信息按鈕不可用,點擊錄入信息按鈕之後可以實現跳轉至圖2所示界面,輸入相應信息提交之後,點擊提交按鈕後又跳轉回圖1所示界面。此時,讀取信息 按鈕變爲可用。
圖1
圖2

實現代碼

MainActivity.java中的代碼

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    Button btnRead;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnRead = (Button)findViewById(R.id.buttonRead);
        btnRead.setEnabled(false);
        Log.i("Activity","MainActivity is on create.");
    }
    
    public void myClickEnter(View view) {
        btnRead.setEnabled(true);
        Intent intent = new Intent(this,EnterActivity.class);
        startActivity(intent);
    }

    public void myClickRead(View view) {
        SharedPreferences sp = getSharedPreferences("mySP",MODE_PRIVATE);
        String s_id = sp.getString("id",null);
        String s_name = sp.getString("name",null);
        String s_gender = sp.getString("gender",null);
        String s_age = sp.getString("age",null);
        String s_like = sp.getString("hobby",null);

        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add(s_id);
        arrayList.add(s_name);
        arrayList.add(s_gender);
        arrayList.add(s_age);
        arrayList.add(s_like);
        TextView textViewShow = (TextView)findViewById(R.id.textViewShow);
        textViewShow.setText(arrayList.toString());
    }
}

EnterActivity中的代碼

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import java.util.ArrayList;

public class EnterActivity extends AppCompatActivity {
    private String gender = "男";
    private ArrayList<String> hobby = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_enter);

        final CheckBox checkBox1 = (CheckBox)findViewById(R.id.checkBox1);
        final CheckBox checkBox2 = (CheckBox)findViewById(R.id.checkBox2);
        final CheckBox checkBox3 = (CheckBox)findViewById(R.id.checkBox3);

        checkBox1.setOnCheckedChangeListener(checkedChangeListener);
        checkBox2.setOnCheckedChangeListener(checkedChangeListener);
        checkBox3.setOnCheckedChangeListener(checkedChangeListener);
    }

    //創建一個狀態改變監聽對象
    private CompoundButton.OnCheckedChangeListener  checkedChangeListener = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked){
                String str = buttonView.getText().toString();
                hobby.add(str);
                Log.i("複選按鈕","選中了{"+str+"}");
                Toast.makeText(EnterActivity.this,"選中了 "+str+" ",Toast.LENGTH_SHORT).show();
            }
        }
    };

    public void myClickCommit(View view) {
        EditText editTextID = (EditText)findViewById(R.id.editTextID);
        EditText editTextName = (EditText)findViewById(R.id.editTextName);
        EditText editTextAge = (EditText)findViewById(R.id.editTextAge);

        String id = editTextID.getText().toString();
        String name = editTextName.getText().toString();
        String age = editTextAge.getText().toString();

        RadioGroup radioGroupGender = (RadioGroup)findViewById(R.id.radioGroupGender);
        radioGroupGender.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                RadioButton radioButton = (RadioButton)findViewById(checkedId);
                gender = radioButton.getText().toString();
            }
        });

        SharedPreferences sharedPreferences = getSharedPreferences("mySP",MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("id", id);
        editor.putString("name", name);
        editor.putString("gender",gender);
        editor.putString("age",age);
        editor.putString("hobby", hobby.toString());
        editor.commit();

        Toast.makeText(EnterActivity.this,"提交成功",Toast.LENGTH_SHORT).show();
        finish();
    }
}

activity_main.xml中的內容

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        android:orientation="vertical"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/textView4"
            android:layout_width="match_parent"
            android:layout_height="100dp" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:orientation="horizontal">

            <Button
                android:id="@+id/buttonEnter"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_marginRight="50dp"
                android:onClick="myClickEnter"
                android:text="錄入信息" />

            <Button
                android:id="@+id/buttonRead"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="myClickRead"
                android:text="讀取信息" />
        </LinearLayout>

        <TextView
            android:id="@+id/textViewShow"
            android:layout_marginTop="50dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text=""
            android:textColor="@android:color/black"
            android:textSize="18sp" />

    </LinearLayout>

activity_enter.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:orientation="vertical">

    <TextView
        android:id="@+id/textView00"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:textColor="@android:color/black"
        android:textSize="18sp" />

    <TableLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal">

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:stretchColumns="0">

            <TextView
                android:id="@+id/textViewID"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="學號"
                android:textColor="@android:color/black" />

            <EditText
                android:id="@+id/editTextID"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:hint="請輸入學號"
                android:textSize="18sp" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/textViewName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="姓名:"
                android:textColor="@android:color/black" />

            <EditText
                android:id="@+id/editTextName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:hint="請輸入姓名"
                android:inputType="textPersonName"
                android:textSize="18sp" />
        </TableRow>

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/textView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="性別:"
                android:textColor="@android:color/black" />

            <RadioGroup
                android:id="@+id/radioGroupGender"
                android:orientation="horizontal"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

                <RadioButton
                    android:checked="true"
                    android:id="@+id/radioButtonM"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="男" />

                <RadioButton
                    android:id="@+id/radioButtonF"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="女" />

            </RadioGroup>

        </TableRow>

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <TextView

                android:textColor="@android:color/black"
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="年齡:" />

            <EditText
                android:hint="請輸入年齡"
                android:id="@+id/editTextAge"
                android:layout_width="160sp"
                android:layout_height="wrap_content"
                android:inputType="number" />



        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/textView21"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="愛好:"
                android:textColor="@android:color/black" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <CheckBox
                    android:id="@+id/checkBox1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="體育" />

                <CheckBox
                    android:id="@+id/checkBox2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="音樂" />

                <CheckBox
                    android:id="@+id/checkBox3"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="美術" />

            </LinearLayout>

        </TableRow>

            <Button
                android:onClick="myClickCommit"
                android:id="@+id/buttonCommit"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="提交" />

    </TableLayout>

</LinearLayout>

Tips

首先,務必注意從EnterActivity跳轉至MainActivity必須要將EnterActivityfinish,否則,使用intent跳轉,會創建一個新的MainActivity。則無法使MainActivity上的讀取信息 按鈕變爲可用。
其次,還有一種方法來實現使讀取信息 按鈕變爲可用,我們可以在MainActivity中覆寫onRestart()函數,在其中enable讀取信息 按鈕。代碼如下:

    @Override
    protected void onRestart(){
        super.onRestart();
        Log.i("Activity","MainActivity is on restart.");
        btnRead = (Button)findViewById(R.id.buttonRead);
        btnRead.setEnabled(true);
    }

因爲根據Android的activity的生命週期,從MainActivity跳轉至EnterActivity再回到MainActivity需要依次調用onPause(),onStop(),onRestart()這些函數,所以在onRestart()函數裏面可以有效地保證按鈕變爲可用。
在這裏插入圖片描述
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章