- SharedPreferences是一种轻型的数据储存方式,其本质是给予XML文件储存key-value键值对数据,通常用来存储一些简单的配置信息。
- SharedPreferences对象本身只能获取数据而不支持存储和修改,存储修改是通过Editor对象实现。
实现SharedPreferences存储的步骤如下:
1).获得SharedPreferences对象;
SharedPreferences prf = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
//SharedPreferences prf = getSharedPreferences("Myprf",MODE_PRIVATE);或用此方法也可获得SharedPreferences对象
2).获得SharedPreferences.Editor对象;
SharedPreferences.Editor editor = prf.edit();
3)通过Editor接口的putxxx方法保存key-value(xxx表示数据类型:String;int..);
editor.putString("name","隔壁老王");
editor.putInt("age",48);
editor.putBoolean("猥琐",true);
4).通过Editor的commit方法保存key-value
editor.commit();
*
每次commit()后其上的操作才有效,通过getxxx()取值。
实例:
activity_main.xml
<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.administrator.sharedpreferencestest.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="昵称:"
android:layout_alignParentLeft="true"
android:id="@+id/name"
android:paddingTop="18dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/name"
android:id="@+id/ipname"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/name"
android:text="密码:"
android:id="@+id/code"
android:paddingTop="22dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/ipcode"
android:layout_toRightOf="@+id/code"
android:layout_below="@+id/ipname"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/login"
android:text="登陆"
android:layout_marginLeft="40dp"
android:layout_marginStart="40dp"
android:layout_below="@+id/ipcode"
android:layout_alignLeft="@+id/ipcode"
android:layout_alignStart="@+id/ipcode"
android:layout_marginTop="50dp"
android:onClick="doClick"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/cancel"
android:text="取消"
android:layout_alignTop="@+id/login"
android:layout_toRightOf="@+id/login"
android:layout_toEndOf="@+id/login"
android:layout_marginLeft="70dp"
android:layout_marginStart="70dp"
android:onClick="doClick"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="保存昵称"
android:id="@+id/sv_name"
android:layout_below="@+id/ipcode"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
EditText ipName,ipCode;
CheckBox sv_name;
Button login,cancel;
SharedPreferences pref;
SharedPreferences.Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ipName = (EditText) findViewById(R.id.ipname);
ipCode = (EditText) findViewById(R.id.ipcode);
sv_name = (CheckBox) findViewById(R.id.sv_name);
login = (Button) findViewById(R.id.login);
cancel = (Button) findViewById(R.id.cancel);
pref = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
editor = pref.edit();
String name = pref.getString("name","");
if (name.equals("")){
sv_name.setChecked(false);
}
else{
sv_name.setChecked(true);
ipName.setText(name);
}
}
public void doClick(View v){
switch (v.getId()){
case R.id.login:
String name = ipName.getText().toString().trim();
String code = ipCode.getText().toString().trim();
if ("admin".equals(name)&&"123456".equals(code)){
if (sv_name.isChecked()){
editor.putString("name",name);
editor.commit();
}
else{
editor.remove("name");
editor.commit();
}
}
else {
Toast.makeText(MainActivity.this,"密码错误",Toast.LENGTH_LONG).show();
}
Toast.makeText(MainActivity.this,"登陆成功",Toast.LENGTH_LONG).show();
break;
}
}
}
第一次启动程序
输入昵称密码并保存昵称
再次启动程序可以看到昵称已被保存
本文主要作为熟悉了解SharedPreferences存储机制及使用方法,此登录注册界面要完善的话可完善的地方不要太多,无奈还很菜,以后能力足够了再专门开登陆界面0.0~
本文为学习笔记,参考慕课网视频整理而来,视频地址:http://www.imooc.com/learn/179。