SharedPreference存储
SharedPreference存储:简单信息
使用步骤:
1、得到SharedPreferences对象
2、调用SharedPreferences对象的edit()方法来获取一个SharedPreferences.Editor对象。
3、向SharedPreferences.Editor对象中添加数据。
4、调用commit方法将添加的数据提交。
MainActivity.java
public class MainActivity extends Activity implements View.OnClickListener
{
private EditText etName,etAge;
private Button btnSave,btnResume;
//共享参数--->本质上就是一个map结构的xml文件
private SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
sp = getSharedPreferences("data",MODE_PRIVATE);
}
private void initView() {
etName = (EditText)findViewById(R.id.name);
etAge = (EditText)findViewById(R.id.age);
btnSave = (Button) findViewById(R.id.save);
btnResume = (Button) findViewById(R.id.resume);
btnSave.setOnClickListener(this);
btnResume.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.save://保存数据
String name = etName.getText().toString().trim();
String age = etAge.getText().toString().trim();
if (TextUtils.isEmpty(name) || TextUtils.isEmpty(age)) {
return;
}
//编辑器
SharedPreferences.Editor editor = sp.edit();
editor.putString("name",name);
editor.putString("age", age);
Boolean commit = editor.commit();
if (commit){
Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show();
etAge.setText("");
etName.setText("");
}
break;
case R.id.resume://恢复数据
String nameValue = sp.getString("name", "");
String ageValue = sp.getString("age","");
etName.setText(nameValue);
etAge.setText(ageValue);
break;
default:
break;
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context="com.example.rrd.sharepreferencedemo.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名" />
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:background="@android:drawable/editbox_background"
android:hint="请输入用户名"
android:padding="5dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="年龄" />
<EditText
android:id="@+id/age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:background="@android:drawable/editbox_background"
android:hint="请输入年龄"
android:padding="5dp"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/save"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="保存参数"/>
<Button
android:layout_marginLeft="10dp"
android:id="@+id/resume"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="恢复参数"/>
</LinearLayout>
</LinearLayout>