共享参数存储 Shared Preferences
1, 特征
1.1 存放轻量级数据的存储方式
1.2 本质上是以xml的格式, 通过键值队的方式对数据进行读取
1.3 通常是用于存储简单的数据信息
1.4 应用程序卸载后, 文件也会被删
2, 存储的数据类型
boolean int string long float
3, 存放数据的路径
data/data/应用程序包名/shared_prefs/***.xml
4, 存储数据
4.1, 得到共享参数对象
SharedPreferences sp = getSharedPreferences(文件名称没有扩展名, 文件的操作模式);
4.2, 得到共享参数的编辑对象
Editor editor = sp.edit();
4.3, 向共享参数中写入数据
editor.putInt(key,value);
4.4, 提交数据
editor.commit();
5, 读取数据
5.1 得到共享参数的对象
SharedPreferences sp = getSharedPreferences(文件名称没有扩展名, 文件的操作模式);
5.2 读取数据
int color = sp.getInt(key,默认的内容);
6, Editor 的常用方法
editor.commit(); 提交数据
editor.clear(); 清除所有数据
editor.remove(key); 移除指定Key的数据
实现用户登录记住密码:
- public class MainActivity extends Activity {
- private EditText namee, pwde;
- private CheckBox check;
- private SharedPreferences sp;
- private Editor editor;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- namee = (EditText) findViewById(R.id.namee);
- pwde = (EditText) findViewById(R.id.pwde);
- check = (CheckBox) findViewById(R.id.check);
-
- sp = getSharedPreferences("loginxml", Context.MODE_PRIVATE);
-
- String name = sp.getString("name", "");
- String pwd = sp.getString("pwd", "");
- if (!("").equals(name) || !("").equals(pwd)) {
- namee.setText(name);
- pwde.setText(pwd);
- check.setChecked(true);
- }
- }
-
- public void login(View v) {
- String name = namee.getText().toString();
- String pwd = pwde.getText().toString();
- if (check.isChecked()) {
-
- editor = sp.edit();
- editor.putString("name", name);
- editor.putString("pwd", pwd);
- editor.commit();
- Toast.makeText(this, "登录成功", 2).show();
-
- } else {
- editor = sp.edit();
-
- editor.remove("pwd");
- editor.commit();
- Toast.makeText(this, "清除成功", 2).show();
- }
- }
- }
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- tools:context=".MainActivity" >
-
- <TextView
- android:id="@+id/tv"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerHorizontal="true"
- android:text="用户登录"
- android:textSize="25sp" />
- <TextView
- android:id="@+id/name"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="用户名 : "
- android:layout_below="@id/tv"
- android:layout_marginTop="20dp"
- android:textSize="20sp" />
- <EditText
- android:id="@+id/namee"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:hint="请输入用户名"
- android:layout_toRightOf="@id/name"
- android:background="@android:drawable/edit_text"
- android:layout_marginLeft="20dp"
- android:layout_alignBottom="@id/name"/>
- <TextView
- android:id="@+id/pwd"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="密码 : "
- android:layout_below="@id/name"
- android:layout_marginTop="30dp"
- android:textSize="20sp" />
- <EditText
- android:id="@+id/pwde"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignLeft="@id/namee"
- android:hint="请输入密码"
- android:layout_toRightOf="@id/pwd"
- android:background="@android:drawable/edit_text"
- android:layout_alignBottom="@id/pwd"/>
- <CheckBox
- android:id="@+id/check"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_below="@id/pwd"
- android:layout_marginTop="20dp"
- android:text="记住密码"/>
- <Button
- android:id="@+id/btn"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_margin="30dp"
- android:onClick="login"
- android:text="登录"
- android:layout_below="@id/check"/>
- <Button
- android:id="@+id/btn01"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_margin="30dp"
- android:text="重置"
- android:layout_toRightOf="@id/btn"
- android:layout_below="@id/check"/>
-
- </RelativeLayout>