[置顶] [Android基础]Android中SharedPreferences的使用

趁着复习Android,就多写写基础博客。

Android的四种数据存储方式:
1、SharedPreferences
2、SQLite
3、ContentProvider
4、File

SharedPreferences:
1、是一种轻型的数据存储方式
2、本质是基于XML文件存储key-value键值对数据
3、通常用来存储一些简单的配置信息

1、SharedPreferences对象本身只能获取数据而不支持存储和修改,存储修改是通过Editor对象实现。
2、实现SharedPreferences存储的步骤如下:
(1)获得SharedPreferences对象
(2)获得SharedPreferences.Editor对象
(3)通过Editor接口的putXxx方法保存key-value对其中Xxx表示不同的数据类型
(4)通过Editor接口的commit方法保存key-value对

通过一个登录记住用户名和密码的案例来分析:
布局代码如下:

<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" tools:context=".MainActivity" >

    <TextView  android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginTop="16dp" android:text="用户名:" />

    <EditText  android:id="@+id/etuserName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignTop="@+id/textView1" android:layout_toRightOf="@+id/textView1" android:ems="10" >
        <requestFocus />
    </EditText>

    <TextView  android:id="@+id/aa" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/etuserName" android:text="密 码" />

    <EditText  android:id="@+id/etuserpass" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/etuserName" android:layout_alignParentRight="true" android:layout_alignTop="@+id/aa" android:password="true" android:ems="10" >


    </EditText>

    <CheckBox  android:id="@+id/chkSaveName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:checked="false" android:layout_below="@+id/etuserpass" android:text="保存用户信息" />

    <Button  android:id="@+id/btnLogin" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/chkSaveName" android:onClick="doClick" android:text="登陆" />

    <Button  android:id="@+id/btnCancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/btnLogin" android:layout_alignBottom="@+id/btnLogin" android:layout_toRightOf="@+id/btnLogin" android:onClick="doClick" android:text="取消" />

</RelativeLayout>

MainActivity.class

public class MainActivity extends AppCompatActivity {

    EditText etUserName, etUserPass;
    CheckBox chk;
    SharedPreferences pref;
    SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        etUserName = (EditText) findViewById(R.id.etuserName);
        etUserPass = (EditText) findViewById(R.id.etuserpass);
        chk = (CheckBox) findViewById(R.id.chkSaveName);
        pref = getSharedPreferences("UserInfo", MODE_PRIVATE);
        editor = pref.edit();
        String name = pref.getString("userName", "");
        String pass = pref.getString("userPassword", "");
        if(name == null)
        {
            chk.setChecked(false);
        }
        else
        {
            chk.setChecked(true);
            etUserName.setText(name);
            etUserPass.setText(pass);
        }
    }

    public void doClick(View view) {
        switch (view.getId()) {
            case R.id.btnLogin:
                String name = etUserName.getText().toString().trim();
                String pass = etUserPass.getText().toString().trim();
                if (name.equals("admin") && pass.equals("123456")) {
                    if(chk.isChecked())
                    {
                        editor.putString("userName", name);
                        editor.putString("userPassword", pass);

                        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;
        }
    }

你可能感兴趣的:(android)