一.SharedPreferences(******)
1.sp介绍
保存少量的数据,且这些数据的格式非常简单。 存储5种原始数据类型: boolean, float, int, long, String
比如应用程序的各种配置信息(如是否打开音效、是否使用震动效果、小游戏的玩家积分等),记住密码功能,音乐播放器播放模式。
存哪了: /data/data/应用程序包名/shared_prefs/Xxx.xml文件,以Key-Value的格式存储
2.如何存储数据
步骤1:得到SharedPreferences对象 getSharedPreferences(“文件的名称”,“文件的类型”);
(1).Context.MODE_PRIVATE:指定该SharedPreferences数据只能被应用程序读写
(2)MODE_APPEND:检查文件是否存在,存在就往文件追加内容,否则就创建新文件。
(3).Context.MODE_WORLD_READABLE:指定该SharedPreferences数据能被其他
应用程序读,但不能写。
(4).Context,MODE_WORLD_WRITEABLE:指定该SharedPreferences数据能被其他应用程序写,但不能读。
步骤2:得到 SharedPreferences.Editor编辑对象
SharedPreferences.Editor editor=sp.edit();
步骤3:添加数据
editor.putBoolean(key,value)
editor.putString()
editor.putInt()
editor.putFloat()
editor.putLong()
步骤4:提交数据 editor.commit()
Editor其他方法: editor.clear() 清除数据 editor.remove(key) 移除指定key对应的数据
3.如何读取数据
步骤1:得到SharedPreferences对象 getSharedPreferences(“文件的名称”,“文件的类型”);
步骤2:读取数据 String msg = sp.getString(key,defValue);
//得到SharedPreferences对象
//参数一 xml文件的名字 参数二 模式 MODE_PRIVATE 指定该SharedPreferences数据只能被本应用程序读写
SharedPreferences adidas = getSharedPreferences("adidas", MODE_PRIVATE);
//直接读取
//参数一 键 参数二 找不到的时候给默认值
String harden = adidas.getString("Harden", "没找到的默认值");
Toast.makeText(MainActivity.this, harden, Toast.LENGTH_SHORT).show();
4.简单登录界面的记住密码
布局代码
<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=".passwordActivity"
android:orientation="vertical"
android:gravity="center">
<EditText
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<RelativeLayout
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:layout_alignParentLeft="true"
android:id="@+id/cb_remember"
android:text="记住密码"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_alignParentRight="true"
android:id="@+id/login"
android:text="登录"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
</LinearLayout>
Activity代码
public class passwordActivity extends AppCompatActivity {
private SharedPreferences sharedPreferences;
private EditText username;
private EditText password;
private CheckBox cb;
private Button login;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_password);
username = findViewById(R.id.username);
password = findViewById(R.id.password);
cb = findViewById(R.id.cb_remember);
login = findViewById(R.id.login);
//TODO 写数据
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String usernametrim = username.getText().toString().trim();
String passwordtrim = password.getText().toString().trim();
//用户名和密码是否正确
if("2350226951".equals(usernametrim)&&"123".equals(passwordtrim)&&cb.isChecked()==true){
//判断记住密码是否被选中
sharedPreferences=getSharedPreferences("NIKE",MODE_PRIVATE);
SharedPreferences.Editor edit = sharedPreferences.edit();
edit.putBoolean("ischeck",true);
edit.putString("username",usernametrim);
edit.putString("password",passwordtrim);
edit.commit();
Toast.makeText(passwordActivity.this, "记住用户名密码成功", Toast.LENGTH_SHORT).show();
}
else{//清空
sharedPreferences=getSharedPreferences("NIKE",MODE_PRIVATE);
SharedPreferences.Editor edit = sharedPreferences.edit();
edit.putBoolean("ischeck",false);
edit.commit();
Toast.makeText(passwordActivity.this, "记住用户名密码失败", Toast.LENGTH_SHORT).show();
}
}
});
//TODO 读取
SharedPreferences nike = getSharedPreferences("NIKE", MODE_PRIVATE);
boolean ischeck = nike.getBoolean("ischeck", false);
if(ischeck){
//读到用户名和密码展现在页面中,复选框被勾选
String string = nike.getString("username", "没找到用户名");
String string1 = nike.getString("password", "没找到密码");
username.setText(string);
password.setText(string1);
cb.setChecked(true);
}
}
}