Android SharedPreferences的轻量级数据存储类的使用 记住密码小案例

今天做一个记住密码的小案例,这里用到了一个非常常用的一个存储类,SharedPreferences类。
首先,我们看一下是怎么保存信息的 。

private SharedPreferences sharedPreferences;
sharedPreferences =getSharedPreferences("rememberpassword", Context.MODE_PRIVATE);//实例化sharedPreferences对象,将信息存在rememberpassword.xml文件中

 SharedPreferences.Editor editor = sharedPreferences.edit();//开启编辑器

editor.putString("name",name); editor.putString("password",password);//name,password已经从TextView中获取了,存入数据

 editor.commit();//最后提交一下。

然后,我们介绍一下怎么读取数据。

private SharedPreferences sharedPreferences;
sharedPreferences =getSharedPreferences("rememberpassword", Context.MODE_PRIVATE);

 String name =sharedPreferences.getString("name","");
            String password = sharedPreferences.getString("password","");
            //将name ,password就读取了

最后附上详细代码。
登录界面

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    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"
    android:background="@color/gray"
    tools:context="com.bzu.cyz.myapplication.MainActivity">

    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="用户名"
    android:textSize="20dp"
    android:id="@+id/textView"
    android:layout_alignBottom="@+id/et_user" />

    <EditText
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:id="@+id/et_user"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/textView"
        android:layout_toEndOf="@+id/textView" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="密 码"
        android:textSize="20dp"
        android:layout_alignBottom="@+id/et_pass"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/textView2" />

    <EditText
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:id="@+id/et_pass"
        android:layout_below="@+id/et_user"
        android:layout_toRightOf="@+id/textView"
        android:layout_toEndOf="@+id/textView"
        android:password="true" />

    <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="登录"
            android:onClick="login"
            android:id="@+id/button"
            android:layout_marginTop="77dp"
            android:layout_below="@+id/et_pass"
            android:layout_centerHorizontal="true"/>

    <CheckBox
            android:text="记住密码"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/cb1"
            android:layout_below="@+id/textView2"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginTop="33dp"/>
</RelativeLayout>

登录按钮设置一个点击事件login。一个很low的页面,大家如果想要好看的点的,可以自己加一下样式。

MainActivity

public class MainActivity extends AppCompatActivity {
    private EditText eName;
    private EditText ePassword;
    private CheckBox checkBox;
    private SharedPreferences sharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        sharedPreferences =getSharedPreferences("rememberpassword", Context.MODE_PRIVATE);
        boolean isremember =sharedPreferences.getBoolean("rememberpassword",false);
        if(isremember){
            String name =sharedPreferences.getString("name","");
            String password = sharedPreferences.getString("password","");
            eName.setText(name);
            ePassword.setText(password);
            checkBox.setChecked(true);

         }

    }

    private void initView() {
        eName = (EditText) findViewById(R.id.et_user);
        ePassword = (EditText) findViewById(R.id.et_pass);
        checkBox = (CheckBox) findViewById(R.id.cb1);

    }
    public  void login(View view){
        String name = eName.getText().toString();
        String password = ePassword.getText().toString();
        if("admin".equals(name)&&"123".equals(password)){
            SharedPreferences.Editor editor = sharedPreferences.edit();
            if(checkBox.isChecked()){
                editor.putBoolean("rememberpassword",true);
                editor.putString("name",name);
                editor.putString("password",password);
            }else {
                editor.clear();
            }
            editor.commit();
            Intent intent = new Intent(this,WelcomeActivity.class);
            startActivity(intent);
            finish();
        }else {
            Toast.makeText(this,"密码账号错误",Toast.LENGTH_LONG).show();
        }
    }

}

多选框被选中时才会保存数据。还有一个跳转。随便写一个WelcomeActivity.java就好。

就这样,当选中记住密码点击登录跳转到另一个页面,数据就会保存到文件中,当关闭应用,再次打开时,会发现数据已经呈现在文本框中了。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值