Android数据存储方式有四种:1)SharedPreferences;2)SQLite;3)Content Provider;4)File
SharedPreferences:
安卓自带的一个轻型存储类,是一种轻型的数据存储方式,本质是基于XML文件存取Key--Value键值对数据,通常用来存储一些简单的配置信息(如窗口状态)。当然配置信息不是不能用数据库来存,只是一般在配置信息很少的情况下使用数据库来存储很不划算,消耗的代价相对而言要更大。同时,SharedPreferences只能存储String,int类型的数据,存储别的类型数据最终也是需要转换为String、int类型才能实现存储。
最终以XML文件的形式存储于Data目录下。
SharedPreferences对象本身只能获取数据而不支持存储和修改,存储和修改通过Editor对象实现。
实现SharedPreferences存储的步骤:1.获得SharedPreferences对象
2.获得SharedPreferences.Editor对象
3.通过Editor接口的putXxx方法保存key-value对(XXX表示不同的数据类型)
4.通过Editor接口的commit方法保存key-value对
SharedPreferences pref=getSharedPreferences("mypref",MODE_PRIVATE);
Editor editor=pref.edit();
editor.putString("name","zs");
editor.commit();
简单运用: 用户名“admin”,密码123456,匹配成功后点击登录打印Toast提示登录,匹配错误则Toast提示“信息错误”,CheckBox可以选择是否保存用户名,若保存则在登录后第二次打开app时显示已保存的用户名,不必重新输入。
源码:
1)布局文件
<TextView
android:id="@+id/textView1"
android:text="用户名:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"/>
<EditText
android:id="@+id/etuserName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/textView1"
android:layout_alignTop="@+id/textView1"
android:layout_alignParentRight="true"
android:ems="10"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密 码:"
android:layout_below="@+id/etuserName"
android:layout_alignParentLeft="true"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/etuserPass"
android:layout_alignParentRight="true"
android:layout_below="@+id/etuserName"
android:layout_alignStart="@+id/etuserName" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="保存用户名"
android:id="@+id/chkSaveName"
android:layout_below="@+id/etuserPass"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录"
android:id="@+id/btnLogin"
android:layout_below="@+id/chkSaveName"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消"
android:id="@+id/btnCancel"
android:layout_alignTop="@+id/btnLogin"
android:layout_toEndOf="@+id/btnLogin" />
2)java文件
public class MainActivity extends Activity {
private EditText etUserName,etUserPass;
private CheckBox chk;
private Button btnLogin,btnCancel;
private SharedPreferences pref;
private Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etUserName= (EditText) findViewById(R.id.etuserName);
etUserPass= (EditText) findViewById(R.id.etuserPass);
btnLogin= (Button) findViewById(R.id.btnLogin);
btnCancel= (Button) findViewById(R.id.btnCancel);
chk= (CheckBox) findViewById(R.id.chkSaveName);
pref=getSharedPreferences("UserInfo",MODE_PRIVATE);
editor=pref.edit();
String name=pref.getString("userName","");
if (name==null){
chk.setChecked(false);
}else {
chk.setChecked(true);
etUserName.setText(name);
}
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name=etUserName.getText().toString().trim();
String pass=etUserPass.getText().toString().trim();
if ("admin".equals(name)&&"123456".equals(pass)){
if (chk.isChecked()){//勾选
editor.putString("userName",name);
editor.commit();
}else {
editor.remove("userName");
editor.commit();
}
Toast.makeText(MainActivity.this,"登录",Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(MainActivity.this,"信息匹配错误",Toast.LENGTH_SHORT).show();
}
}
});
}
}