[Android]資料篇,android資料
轉載請標註:轉載於http://www.cnblogs.com/Liuyt-61/p/6637515.html
---------------------------------------------------------------
Android資料的四種儲存方式:
1、SharedPreferences
2、SQLite
3、Content Provider
4、File
----------------------分割線----------------------------------
一、SharedPreferences:
1.是一種輕型資料存放區方式.
2.本質是基於XML檔案儲存體 key-value 索引值對資料
3.通常用來儲存一些簡單的配置資訊,如使用者名稱、密碼(後面附上執行個體代碼)
1>SharedPreferences對象本身只能擷取資料而不支援儲存和修改
Editor實現儲存和修改
2>實現儲存的步驟:
①使用Activity類的getSharedPreferences擷取其對象,其中儲存key-value的檔案的名稱由getSharedPreferences方法第一個參數指定。
②使用SharedPreferences介面的Edit獲得SharedPreferences.Editor對象。
③通過SharedPreferences.Editor介面的put×××方法儲存key-value對
④通過SharedPreferences.Editor介面的commit()方法儲存key-value對進行提交。
直接上執行個體代碼(登入介面儲存使用者名稱)
main.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" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/tv_username" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="使用者名稱" /> <EditText android:id="@+id/et_username" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:ems="10" android:hint="input username" android:inputType="textPersonName" > </EditText> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/tv_password" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密 碼" /> <EditText android:id="@+id/et_password" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:ems="10" android:hint="input password" android:inputType="textPassword" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <CheckBox android:id="@+id/cb_remember" android:checked="false" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="記住使用者名稱" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/btn_login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="doClick" android:text="登入" /> <Button android:id="@+id/btn_canel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="doClick" android:text="取消" /> </LinearLayout></LinearLayout>
MainActivity.java
package com.Liuyt.s03_e28_sharedpreferences;import android.app.Activity;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.CheckBox;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends Activity { private Button btn_login,btn_canel; private EditText et_username,et_password; private CheckBox cb_remenber; private Editor editor; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);// SharedPreferences pref = getSharedPreferences("myPref", MODE_PRIVATE);// Editor editor = pref.edit();// editor.putString("name", "Liuyt");// editor.putInt("age",18);// editor.commit();// System.out.println(pref.getString("name", ""));// System.out.println(pref.getInt("age", 0)); btn_canel = (Button) findViewById(R.id.btn_canel); btn_login = (Button) findViewById(R.id.btn_login); et_username = (EditText) findViewById(R.id.et_username); et_password = (EditText) findViewById(R.id.et_password); cb_remenber = (CheckBox) findViewById(R.id.cb_remember); SharedPreferences pref = getSharedPreferences("myPref1", MODE_PRIVATE); editor = pref.edit(); String name = pref.getString("username", ""); if(name == null){ cb_remenber.setChecked(false); }else{ cb_remenber.setChecked(true); et_username.setText(name); } } public void doClick(View view){ switch (view.getId()) { case R.id.btn_login: String name = et_username.getText().toString().trim();//去掉首尾空格 String password = et_password.getText().toString().trim(); if("admin".equals(name)&&"123456".equals(password)){ if(cb_remenber.isChecked()){ editor.putString("username", name); editor.commit(); }else{ editor.remove("username"); editor.commit(); } Toast.makeText(MainActivity.this, "登入成功", Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(MainActivity.this, "拒絕登入", Toast.LENGTH_SHORT).show(); } break; default: break; } } }