**Android数据存储**
数据存储:
1 . SharedPreferences(以键值对的方式保存的到.xml文件中 路径在手机Data-Data目录下 通过key来获取数据) 一般用于简单的收藏 写在这的坏处:不能同步
用于:记住密码 自动登录
创建SharedPreferences:
SharedPreferences sp=getSharedPreferences(“myopt”,Context.MODE_PRIVATE);
myopt文件名
2 . 读写SD卡
3 . SQLite(轻量级的数据库 文件名为.db )
4 . ContentProvider(内容提供者 内容来自于SQLite 是一个标准的接口)
存储实例:
SharedPreferencesActivity代码:
public class SharedPreferencesActivity extends AppCompatActivity {
private EditText user, pwd;
private Button login;
private CheckBox cb1, cb2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shared_preferences);
user = (EditText) findViewById(R.id.user);
pwd = (EditText) findViewById(R.id.pwd);
login = (Button) findViewById(R.id.login);
cb1 = (CheckBox) findViewById(R.id.cb1);
cb2 = (CheckBox) findViewById(R.id.cb2);
final SharedPreferences sp = getSharedPreferences("userInfo", MODE_PRIVATE);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences.Editor editor = sp.edit();
editor.putString("userName", user.getText().toString());
//字符串转成Int
editor.putInt("userPwd", Integer.parseInt(pwd.getText().toString()));
editor.putBoolean("cb1", cb1.isChecked());
editor.putBoolean("cb2", cb2.isChecked());
editor.commit();
Intent intent = new Intent(SharedPreferencesActivity.this,
SettingActivity.class);
startActivity(intent);
}
});
}
@Override
protected void onStart() {
super.onStart();
SharedPreferences sp1 = getSharedPreferences("switch", MODE_PRIVATE);
boolean switch1 = sp1.getBoolean("switch1", false);
boolean switch2 = sp1.getBoolean("switch2", false);
cb1.setChecked(switch1);
cb2.setChecked(switch2);
}
}
activity_shared_preferences布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SharedPreferencesActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/user"
android:hint="请输入用户名:"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/user"
android:id="@+id/pwd"
android:hint="请输入密码:"/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="记住密码"
android:id="@+id/cb1"
android:checked="false"
android:layout_below="@+id/pwd"
android:layout_alignParentStart="true"
android:layout_marginStart="46dp" />
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="自动登录"
android:id="@+id/cb2"
android:layout_marginStart="237dp"
android:layout_below="@+id/pwd"
android:layout_alignParentStart="true" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/login"
android:text="登录"
android:layout_below="@+id/cb1"
android:layout_alignParentStart="true" />
</RelativeLayout>
SettingActivity 代码:
public class SettingActivity extends AppCompatActivity {
private Switch switch1, switch2;
private Button save;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting);
switch1 = (Switch) findViewById(R.id.switch1);
switch2 = (Switch) findViewById(R.id.switch2);
save = (Button) findViewById(R.id.save);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences sp1 = getSharedPreferences("switch", MODE_PRIVATE);
SharedPreferences.Editor editor = sp1.edit();
editor.putBoolean("switch1", switch1.isChecked());
editor.putBoolean("switch2", switch2.isChecked());
editor.commit();
Intent intent = new Intent(SettingActivity.this, SharedPreferencesActivity.class);
startActivity(intent);
}
});
}
@Override
protected void onStart() {
super.onStart();
SharedPreferences sp = getSharedPreferences("userInfo", MODE_PRIVATE);
final boolean cb1 = sp.getBoolean("cb1", false);
final boolean cb2 = sp.getBoolean("cb2", false);
switch1.setChecked(cb1);
switch2.setChecked(cb2);
}
}
activity_setting布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.dell.jreduch08.SettingsActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"></LinearLayout>
<Switch
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="记住密码"
android:id="@+id/switch1"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:checked="false" />
<Switch
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="自动登录"
android:id="@+id/switch2"
android:checked="false"
android:layout_below="@+id/switch1"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="保存"
android:id="@+id/save"
android:layout_below="@+id/switch2"
android:layout_centerHorizontal="true" />
</RelativeLayout>
实现效果:
改变选择后能与登录界面的选择框保持同步: