使用SharedPreferences实现登录时记住密码的功能

使用SharedPreferences向文件中写入数据:无需权限

// 通过SharedPreferences获取editor向data.xml中写入数据
            SharedPreferences.Editor editor = getSharedPreferences("data.xml", MODE_PRIVATE).edit();
            editor.putString("name", "cc");
            editor.putBoolean("married", true);
            editor.putInt("age", 125);
            editor.commit();

使用SharedPreferences从文件中读取数据:

// 读取数据
            SharedPreferences sp = getSharedPreferences("data.xml", MODE_PRIVATE);
            String name = sp.getString("name", "pepelu");
            boolean married = sp.getBoolean("married", false);
            int age = sp.getInt("age", 0);
            Toast.makeText(this, name + married + age, Toast.LENGTH_SHORT).show();

上面是SharedPreferences的简单使用,下面是使用SharedPreferences实现记住登录信息:

1.layout布局文件:

<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="com.example.sharedpreferences.LoginActivity" >

    <TextView
        android:id="@+id/tv_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="name" />

    <EditText
        android:id="@+id/et_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_toRightOf="@+id/tv_password"
        android:ems="10"
        android:hint="input name" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/tv_password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/tv_name"
        android:layout_below="@+id/tv_name"
        android:layout_marginTop="53dp"
        android:text="password" />

    <EditText
        android:id="@+id/et_password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/tv_password"
        android:layout_toRightOf="@id/tv_password"
        android:ems="10"
        android:hint="input password"
        android:inputType="textPassword" />

    <CheckBox
        android:id="@+id/cb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/et_name"
        android:layout_below="@+id/tv_password"
        android:layout_marginTop="32dp"
        android:text="记住密码" />

    <Button
        android:id="@+id/btn_login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/et_password"
        android:layout_below="@+id/cb"
        android:layout_marginLeft="26dp"
        android:layout_marginTop="62dp"
        android:text="登录" />

</RelativeLayout>
View Code

2.Activity代码:

public class LoginActivity extends Activity {
    private Button login;
    private CheckBox cb;
    private EditText name;
    private EditText password;
    private SharedPreferences sharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        // 初始化控件
        login = (Button) findViewById(R.id.btn_login);
        cb = (CheckBox) findViewById(R.id.cb);
        name = (EditText) findViewById(R.id.et_name);
        password = (EditText) findViewById(R.id.et_password);
        sharedPreferences = getSharedPreferences("login.xml", MODE_PRIVATE);
        // 通过SharedPreferences从读取文件
        String nameSP = sharedPreferences.getString("name", "");
        String passwordSP = sharedPreferences.getString("password", "");
        boolean isCheckedSP = sharedPreferences.getBoolean("check", false);
        // 设置控件初始值
        name.setText(nameSP);
        password.setText(passwordSP);
        cb.setChecked(isCheckedSP);
        // login事件监听
        login.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // 获取user输入值
                String namesString = name.getText().toString();
                String passwordString = password.getText().toString();
                // 登录判断
                if (namesString.equals("cc") && passwordString.equals("12345")) {
                    // checkbox监听
                    if (cb.isChecked()) {
                        // 编辑login.xml
                        SharedPreferences.Editor editor = getSharedPreferences("login.xml", MODE_PRIVATE)
                                .edit();
                        editor.putString("name", namesString);
                        editor.putString("password", passwordString);
                        editor.putBoolean("check", true);
                        
                    } else {
                        // 如果checkbox未选中并且登录验证通过,则把login.xml中储存内容清除
                        sharedPreferences.edit().clear();
                    }
             // 提交
            editor.commit();
// 登录成功提示 Toast.makeText(LoginActivity.this, "login success!", Toast.LENGTH_SHORT).show(); } else { // login fail Toast.makeText(LoginActivity.this, "name or password is wrong!!", Toast.LENGTH_SHORT) .show(); } } }); } }
原文地址:https://www.cnblogs.com/mada0/p/4837896.html