SharePreference的简单用法

public class MainActivity extends AppCompatActivity {
    EditText ueditText, peditText;
    CheckBox checkBox;
    Button button;
    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    private GoogleApiClient client;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ueditText = (EditText) findViewById(R.id.ed_user);
        peditText = (EditText) findViewById(R.id.ed_password);
        checkBox= (CheckBox) findViewById(R.id.cb_remember);
        button= (Button) findViewById(R.id.button);
        //使用SharePreferences取出保存的数据,并把数据显示在手机屏幕上
        //初始化数据
        SharedPreferences sharedPreferences=getSharedPreferences("config",0);
        //取出数据,如果取出的数据时空时,只需把getString("","")第二个参数设置成空字符串就行了,不用在判断
        String name=sharedPreferences.getString("name","");
        String password=sharedPreferences.getString("password","");
        //获取勾选的状态
        boolean checkbox=sharedPreferences.getBoolean("checkbox",false);
        ueditText.setText(name);
        peditText.setText(password);
        checkBox.setChecked(checkbox);
    }
    //使用Sharepreferences进行保存数据
   public void login(View view){
       //获取密码和用户名
       String username=ueditText.getText().toString();
       String passwowrd=peditText.getText().toString();
       //文本判断是否为空,新的API:TextUtils.isEmty()
       if (TextUtils.isEmpty(username)&&TextUtils.isEmpty(passwowrd)){
           Toast.makeText(MainActivity.this,"用户名和密码不能为空",Toast.LENGTH_LONG).show();
       }else{
           System.out.println("以后补上");
           if (checkBox.isChecked()){
               //把密码和用户名存起来
               //getSharedPreferences(name,model);,name 会生成一个xml文件,model :模式,可读可写等模式
               SharedPreferences sp=getSharedPreferences("config",0);
               SharedPreferences.Editor editor=sp.edit();
               //把数据进行保存
               editor.putString("name",username);
               editor.putString("password",passwowrd);
               //记住勾选的状态
               editor.putBoolean("checkbox",checkBox.isChecked());
               //提交数据
               editor.commit();
           }else{
               Toast.makeText(MainActivity.this,"未勾选",Toast.LENGTH_LONG).show();
           }
       }
   }
    /**
     * Sharepreference使用的步骤
     * 1.获取sp的实例
     * Sharepreference sp=getSharepreference(name,model);
     * 2.获取编辑器
     * Editor editor=sp.edit();
     * 3.存数据
     * editor.putString(name,值)
     * 4.提交
     * editor.commit();
     */
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值