使用 sharepreferences存储用户偏好设置和回显代码如下:
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.lin.sharepreference.MainActivity"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/name"/> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/name"/> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/age"/> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/age"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button" android:onClick="save"/> </LinearLayout>
MainActivity.java
package com.example.lin.sharepreference; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.Toast; import com.example.lin.service.PreferenceService; import java.util.Map; public class MainActivity extends AppCompatActivity { private EditText nameText; private EditText ageText; private PreferenceService service; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); nameText=(EditText)this.findViewById(R.id.name); ageText= (EditText)this.findViewById(R.id.age); service=new PreferenceService(this); //回显示 Map<String,String> params=service.getPreferences(); nameText.setText(params.get("name")); ageText.setText(params.get("age")); } public void save(View v){ String name=nameText.getText().toString(); String age=ageText.getText().toString(); service.save(name,Integer.valueOf(age)); Toast.makeText(getApplicationContext(),R.string.success,Toast.LENGTH_SHORT).show(); } }
package com.example.lin.service; import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.widget.EditText; import java.util.HashMap; import java.util.Map; /** * Created by lin on 2016/6/21. */ public class PreferenceService { private Context context; public PreferenceService(Context context) { this.context = context; } /** * 保存用户数据 * @param name 姓名 * @param age 年龄 */ public void save(String name, Integer age) { SharedPreferences prefercences=context.getSharedPreferences("lin",Context.MODE_PRIVATE);//第一个参数代表名称,第二参数代表操作模式 Editor editor=prefercences.edit(); editor.putString("name",name); editor.putInt("age",age); editor.commit(); } /** *获取各项配置参数 * @return */ public Map<String,String> getPreferences(){ Map<String,String> params=new HashMap<String,String>(); SharedPreferences prefercences=context.getSharedPreferences("lin",Context.MODE_PRIVATE); params.put("name",prefercences.getString("name","")); params.put("age",String.valueOf(prefercences.getInt("age",0))); return params; } }
String.xml
<resources> <string name="app_name">软件参数设置</string> <string name="name">姓名</string> <string name="age">年龄</string> <string name="button">保存参数</string> <string name="success">保存成功</string> </resources>sharepreferences保存的数据路径:/data/data/package/shared_prefs/名称.xml