1. 簡介幾種數據存儲方式的各自特點。2. 使用SharedPreFerences保存用戶登錄的信息,並且在用戶下次登錄時自動填寫用戶已經保存的信息。

1. 簡介幾種數據存儲方式的各自特點。
Android中有5種數據存儲方式,分別爲文件存儲、SQLite數據庫、SharedPreferences、ContentProvider、網絡。文件存儲方式是一種較常用的方法,在Android中讀取/寫入文件的方法,與Java中實現I/O的程序是完全一樣的,提供openFileInput()和openFileOutput()方法來讀取設備上的文件。SQLite是Android所集成的一個輕量級的嵌入式數據庫,它不僅可以使用Andorid API操作,同時它也支持SQL語句進行增刪改查等操作。SharedPreferences是Android提供的用於存儲一些簡單配置信息的一種機制,採用了XML格式將數據存儲到設備中。不僅可以在同一個包下使用,還可以訪問其他應用程序的數據,但是由於SharedPreferences的侷限性,在實際操作中很少用來讀取其他應用程序的數據。ContentProvider主要用於不同應用程序之間共享數據,ContentProvider更好的提供了數據共享接口的統一性,使不同應用共享數據更規範和安全。網絡存儲數據,通過網絡上提供的存儲空間來上傳(存儲)或下載(獲取)我們存儲在網絡空間中的數據信息。

2. 使用SharedPreFerences保存用戶登錄的信息,並且在用戶下次登錄時自動填寫用戶已經保存的信息。
剛剛打開的界面(圖1)②輸入賬號和密碼,並且勾選記住密碼,點擊登錄後的界面(圖2)③關閉界面,再次打開後的界面(圖3)④取消勾選記住密碼,點擊登錄,關閉界面重新打開後的界面(圖4)。
在這裏插入圖片描述
圖1
在這裏插入圖片描述
圖2
在這裏插入圖片描述
圖3
在這裏插入圖片描述
圖4

Xml:

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

    <RelativeLayout
        android:layout_marginTop="200dp"
        android:gravity="center"
        android:layout_gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <EditText
            android:id="@+id/edit_username"
            android:hint="輸入賬號"
            android:layout_gravity="center"
            android:layout_width="250dp"
            android:layout_height="50dp" />

    </RelativeLayout>
    <RelativeLayout
        android:gravity="center"
        android:layout_gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <EditText
            android:id="@+id/edit_password"
            android:hint="輸入密碼"
            android:password="true"
            android:layout_gravity="center"
            android:layout_width="250dp"
            android:layout_height="50dp" />

    </RelativeLayout>

    <Button
        android:id="@+id/main_button"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:text="登錄"
        />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="40dp">

        <CheckBox
            android:id="@+id/Cb"
            android:layout_width="30dp"
            android:layout_height="20dp"
            android:layout_marginLeft="60dp"
            />

        <TextView
            android:textColor="#000000"
            android:layout_width="80dp"
            android:layout_height="30dp"
            android:text="記住密碼"
            />
        <CheckBox
            android:id="@+id/zidong_check"
            android:layout_width="30dp"
            android:layout_height="20dp"
            android:layout_marginLeft="60dp"
            />

        <TextView
            android:textColor="#000000"
            android:layout_width="80dp"
            android:layout_height="30dp"
            android:text="自動登錄"
            />
    </LinearLayout>

</LinearLayout>


Java:

public class MainActivity extends AppCompatActivity {
    private EditText edit_username;
    private EditText edit_password;
    private CheckBox checkBox;
    private Button button;
    private CheckBox checkBox_zidong;
    private int remamberFlag = 0;
    private String password = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        binID();
        SharedPreferences sharedPreferences = getSharedPreferences("test", MODE_PRIVATE);


        //如果不爲空
        if (sharedPreferences != null) {
            String name = sharedPreferences.getString("name", "");
            password = sharedPreferences.getString("password", "");
            remamberFlag = sharedPreferences.getInt("remeber_flag", 0);
            edit_username.setText(name);
        }

        //確定爲1獲取 記住密碼,打鉤
        if (remamberFlag == 1) {
            checkBox.setChecked(true);
            edit_password.setText(password);
        }
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //1 創建 SharePreferences 對象
                String username = edit_username.getText().toString();
                String password = edit_password.getText().toString();
                SharedPreferences sharedPreferences = getSharedPreferences("test", MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                //2  創建Editor對象,寫入值
                editor.putString("name", username);
                if (checkBox.isChecked()) {
                    remamberFlag = 1;
                    editor.putInt("remeber_flag", remamberFlag);
                    editor.putString("password", password);
                } else {
                    remamberFlag = 0;
                    editor.putInt("remeber_flag", remamberFlag);
                }
                //3  提交
                editor.commit();
                Toast.makeText(MainActivity.this, "登錄成功", Toast.LENGTH_LONG).show();
            }
        });
    }
    private void binID() {
        edit_username = findViewById(R.id.edit_username);
        edit_password = findViewById(R.id.edit_password);
        checkBox = findViewById(R.id.Cb);
        checkBox_zidong = findViewById(R.id.zidong_check);
        button = findViewById(R.id.main_button);

    }
}

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