[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(); //