您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
19_sharedpreference的使用
发布时间:2021-02-08 17:37:12编辑:雪饮阅读()
SharedPreferences的存储 然后运行项目后保存后的文件是一个xml文件,xml中的数据及结构和程序里存储时一一对应,是一个map集合,xml文件位于当前项目所在包的shared_prefs中
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity; import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText mEtName;
private EditText mEtPwd;
private Button mBtLogin;
private CheckBox mcb;
//用来保存参数的接口
private SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mEtName=(EditText) this.findViewById(R.id.et_name);
mEtPwd=(EditText) this.findViewById(R.id.et_pwd);
mBtLogin=(Button) this.findViewById(R.id.bt_login);
mcb=(CheckBox) this.findViewById(R.id.cb_remember_pwd);
mBtLogin.setOnClickListener(this);
//初始化sharedPreferences
sp=this.getSharedPreferences("config.txt", Context.MODE_PRIVATE);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_login:
String name = mEtName.getText().toString().trim();
String pwd = mEtPwd.getText().toString().trim();
SharedPreferences.Editor editor=sp.edit();
editor.putString("name",name);
editor.putString("pwd",pwd);
editor.putBoolean("issetup",true);
//既然使用commit操作,其实SharedPreferences是以事务进行操作的
editor.commit();
Toast.makeText(this, "登陆中....", Toast.LENGTH_LONG).show();
break;
}
}
}上面将SharedPreferences数据写入了,接下来就像前面一样app一打开就直接读取name和pwd到输入框中,则对于SharedPreferences的读取实现如:
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText mEtName;
private EditText mEtPwd;
private Button mBtLogin;
private CheckBox mcb;
private SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mEtName=(EditText) this.findViewById(R.id.et_name);
mEtPwd=(EditText) this.findViewById(R.id.et_pwd);
mBtLogin=(Button) this.findViewById(R.id.bt_login);
mcb=(CheckBox) this.findViewById(R.id.cb_remember_pwd);
mBtLogin.setOnClickListener(this);
sp=this.getSharedPreferences("config.txt", Context.MODE_PRIVATE);
/*
从刚才编辑器中存储的map集合中取出指定键,需要根据对应键存储的类型来取,
一般取对应键的值时候所用方法的第二个参数是当取该键对应值时候发现值不存在时候所返回的默认值
*/
boolean issetup=sp.getBoolean("issetup",false);
if(issetup){
Toast.makeText(this, "issetup值为true", Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(this, "issetup值为false", Toast.LENGTH_LONG).show();
}
String name=sp.getString("name","");
String pwd=sp.getString("pwd","");
mEtName.setText(name);
mEtPwd.setText(pwd);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_login:
String name = mEtName.getText().toString().trim();
String pwd = mEtPwd.getText().toString().trim();
SharedPreferences.Editor editor=sp.edit();
editor.putString("name",name);
editor.putString("pwd",pwd);
editor.putBoolean("issetup",true);
editor.commit();
Toast.makeText(this, "登陆中....", Toast.LENGTH_LONG).show();
break;
}
}
}然后一运行app就读取到了
关键字词:android,sharedpreference
上一篇:18_知识点怎么获取sd卡的大小
下一篇:20_知识点补充