Android系統中主要提供了三種方式用於簡單地實現數據持久化功能,即:
1.文件存儲
2.SharedPreference存儲(首選項存儲) // 用戶可以按照自己的喜好來更改界面風格,操作習慣,常用列表等數據信息,通常把這些信息叫做首選項信息,這些信息基本都是以鍵值對(key-value)的形式來存儲的。
3.數據庫存儲
對於用戶名這種超輕量級數據,SharedPreference存儲再適合不過。
至於SharedPreference的用法這裏不過多介紹,來看看他在實例應用的用法吧。
①創建xml文件進行界面佈局(這裏只是簡單地demo,佈局很隨意~~)
activity_loading.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="用戶名:"
android:id="@+id/textView"
android:layout_alignBottom="@+id/editUserName"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editUserName"
android:layout_gravity="right"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/textView2"
android:layout_toEndOf="@+id/textView2"
android:layout_marginTop="42dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="密 碼:"
android:id="@+id/textView2"
android:layout_marginTop="71dp"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="@+id/editUserPassword"
android:layout_gravity="center_horizontal"
android:layout_alignTop="@+id/textView2"
android:layout_toRightOf="@+id/textView2"
android:layout_toEndOf="@+id/textView2" />
<Button
android:onClick="doClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登錄"
android:id="@+id/btnLogin"
android:layout_alignTop="@+id/btnRegister"
android:layout_alignLeft="@+id/checkBox"
android:layout_alignStart="@+id/checkBox" />
<Button
android:onClick="doClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="註冊"
android:id="@+id/btnRegister"
android:layout_marginRight="53dp"
android:layout_marginEnd="53dp"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="記住用戶名"
android:id="@+id/checkBox"
android:checked="false"
android:layout_above="@+id/btnLogin"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
②Main_Activity.java實現記住賬號這個功能
package com.example.loading;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText editUserName,editUserPassword;
Button btnLogin,btnRegister;
SharedPreferences pref;
SharedPreferences.Editor editor;
CheckBox checkBox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_loading);
//默認
// SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
//自定義
// SharedPreferences pref = getSharedPreferences("myPref",MODE_PRIVATE);
// //得到編輯器對象
// SharedPreferences.Editor editor = pref.edit();
// editor.putString("name","張三");
// editor.commit();
editUserName = (EditText) findViewById(R.id.editUserName);
editUserPassword = (EditText) findViewById(R.id.editUserPassword);
btnLogin = (Button) findViewById(R.id.btnLogin);
btnRegister = (Button) findViewById(R.id.btnRegister);
checkBox = (CheckBox) findViewById(R.id.checkBox);
//實例化SharedPreferences對象 指定MODE_PRIVATE模式還有MODE_WORLD_READABLE MODE_WORLD_WRITEABLE模式
pref = getSharedPreferences("UserInfo",MODE_PRIVATE);
//啓用edit()獲取實例
editor = pref.edit();
//第一個參數爲取到的用戶名名字,第二個參數爲第一個參數的默認值
String name = pref.getString("editUserName",null);
//如果取到的name爲空(第一次啓動程序或者沒有點記住用戶名)單選框false狀態,用戶名的文本框爲空
// 否則true並將取到的name寫入用戶名的文本框
if (name == null){
checkBox.setChecked(false);
}
else {
checkBox.setChecked(true);
//把值放進去
editUserName.setText(name);
}
}
// 兩個按鈕的點擊事件
public void doClick(View view){
switch (view.getId()){
//如果點擊了登錄按鈕
case R.id.btnLogin:
//取出用戶名和密碼,trim()是去掉前後的空格
String userName = editUserName.getText().toString().trim();
String userPassword= editUserPassword.getText().toString().trim();
// Log.i("test",userName);
// Log.i("test",userPassword);
if("admin".equals(userName) && "123456".equals(userPassword)){
//如果單選框被選中,則將取到的用戶名存到editUserName(用戶名文本框)
// 否則移除文本框的內容
if (checkBox.isChecked()){
editor.putString("editUserName",userName);//這是一個鍵值對
//注意提交
editor.commit();
}else{
editor.remove("editUserName");
//注意提交
editor.commit();
}
Toast.makeText(MainActivity.this,"登錄成功",Toast.LENGTH_LONG).show();
}else{
Toast.makeText(MainActivity.this,"賬號/密碼錯誤,登錄失敗",Toast.LENGTH_LONG).show();
}
break;
default:
break;
}
}
}
單機登錄之後就可以將key和value的值存儲在首選項的信息數據文件中,這個文件的目錄是“/data/data/<package_name>/share_prefs”下可以下載下來看看看內容。