SharedPreferences存储数据之用户名实例

checkbox勾选 成功登陆则下次启动时会将之前的用户名保存下来,登陆失败和未勾选则不会保存用户名
这里写图片描述
这里写图片描述
MAinActivity.class

package com.superxingyun.sharedpreferences;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    EditText ed_username;
    EditText ed_password;
    CheckBox chk_checkBox;
    SharedPreferences sharedPreferences;
    SharedPreferences.Editor editor;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        chk_checkBox = (CheckBox) findViewById(R.id.checkBox);

        ed_username = (EditText) findViewById(R.id.ed_username);
        ed_password = (EditText) findViewById(R.id.ed_userpassword);

        sharedPreferences = getSharedPreferences("UserInfo", MODE_PRIVATE);
        editor =  sharedPreferences.edit();

        String name = sharedPreferences.getString("Username", "");
        if (name == null){
            chk_checkBox.setChecked(false);
        }
        else {
            chk_checkBox.setChecked(true);
            ed_username.setText(name);
        }
    }

    public void doClick(View view) {
        switch (view.getId()){
            case R.id.btn_login:
                String name = ed_username.getText().toString().trim();
                String pass = ed_password.getText().toString().trim();
                if ("admin".equals(name) && "123456".equals(pass)){
                    if (chk_checkBox.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;
        }
    }
}

昨天晚上弄完之后发现用户名和密码都匹配的情况下无论checkbox是否勾选再起启动时用户名都无法保存,后来才发现是String name = sharedPreferences.getString(“Username”, “”);这句话中传入的参数username的开头没有大写导致无论怎么取都是空。
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    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.superxingyun.sharedpreferences.MainActivity">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:orientation="vertical">

        <TextView
            android:id="@+id/txt_logo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/txt_logo"
            android:textSize="35sp" />

    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="25dp"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/txt_username"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/txt_username" />

        <EditText
            android:id="@+id/ed_username"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:singleLine="true" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="25dp"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/txt_userpassword"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/txt_password" />

        <EditText
            android:id="@+id/ed_userpassword"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:singleLine="true" />

    </LinearLayout>

    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="25dp"
        android:text="@string/chk_remusername" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="25dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn_login"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="doClick"
            android:text="@string/btn_login" />

        <Button
            android:id="@+id/btn_cancel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="doClick"
            android:text="@string/txt_cancel" />

    </LinearLayout>

</LinearLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值