SharePreference简单的使用

1.xml布局
这里是一个模拟记住账号和密码操作

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity_Share"
    android:orientation="vertical"
    >
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="用户名"
        android:id="@+id/end_1"
        />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/end_2"
        android:hint="密码"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="确定"
        android:id="@+id/bnt1"
        />
</LinearLayout>

SharedPreferences类,它是一个轻量级的存储类。
存储信息到SharedPreferences的使用步骤
1.获取输入框的内容

String muser = end1.getText().toString();
String mpwd = end2.getText().toString();

2…获取SharePreference对象

//获取SharePreference对象(参数1:文件名,参数2:模式)
SharedPreferences share = getSharedPreferences("myshare",MODE_PRIVATE);

3.获取Editor对象

SharedPreferences.Editor edr = share.edit();

4.存储信息

//存储信息(参数1“key”,参数2:值)
edr.putString("user",muser);
edr.putString("pwd",mpwd);

5.指定提交操作

edr.commit();

读取SharedPreferences的使用步骤
其实和存储的类型
1.获取SharePreference对象

//获取SharePreference对象(参数1:文件名,参数2:模式)
SharedPreferences share = getSharedPreferences("myshare",MODE_PRIVATE);

2.根据key读取代码

//根据key读取代码(参数1:key,参数2:指定对应key不存在时,返回参数2的内容作为默认值)
String musers = share.getString("user","");
String mpwds = share.getString("pwd","");

3.显示在页面上

//这里先记得初始化EditText
end1.setText(musers);
end2.setText(mpwds);

完整代码

public class MainActivity_Share extends AppCompatActivity {
    private EditText end1,end2;
    private Button mbnt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main__share);
        initView();
    }

    private void initView() {
        end1 = (EditText)findViewById(R.id.end_1);
        end2 = (EditText) findViewById(R.id.end_2);
        //SharePreference读取
        //1.获取SharePreference对象(参数1:文件名,参数2:模式)
        SharedPreferences share = getSharedPreferences("myshare",MODE_PRIVATE);
        //2.根据key读取代码(参数1:key,参数2:指定对应key不存在时,返回参数2的内容作为默认值)
        String musers = share.getString("user","");
        String mpwds = share.getString("pwd","");
        //显示在页面上
        end1.setText(musers);
        end2.setText(mpwds);

        mbnt = findViewById(R.id.bnt1);
        mbnt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //1.获取输入框的内容
                String muser = end1.getText().toString();
                String mpwd = end2.getText().toString();
                //2.验证
                if (muser.equals("admin") && mpwd.equals("admin")){
                    //2.1存储信息到SharePreference
                    //1.获取SharePreference对象(参数1:文件名,参数2:模式)
                    SharedPreferences share = getSharedPreferences("myshare",MODE_PRIVATE);
                    //2.获取Editor对象
                    SharedPreferences.Editor edr = share.edit();
                    //3.存储信息(参数1“key”,参数2:值)
                    edr.putString("user",muser);
                    edr.putString("pwd",mpwd);
                    //清空edr.clear();
                    //4.指定提交操作
                    edr.commit();
                    Toast.makeText(MainActivity_Share.this,"登录成功",Toast.LENGTH_SHORT).show();
                }else {
                    //2.2验证失败,提醒用户
                    Toast.makeText(MainActivity_Share.this,"账号或者密码错误!",Toast.LENGTH_SHORT).show();
                }

            }
        });
    }
}

成果
在这里插入图片描述
输入之后点击确定之后会记住当前账号和密码,返回在进入不会被清除.
Android萌新,有问题望大佬指出,谢谢

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值