溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

SharedPreferences保存、顯示用戶偏好設置

發(fā)布時間:2020-07-21 03:38:33 來源:網絡 閱讀:1143 作者:ghcomeon 欄目:移動開發(fā)

例如要保存用戶的姓名和年齡兩個參數,如下圖布局

SharedPreferences保存、顯示用戶偏好設置

1.在strings.xml文件中聲明要使用到的字符串


SharedPreferences保存、顯示用戶偏好設置

2.在布局文件中添加<TextView/>,<EditText/>,<Button />控件,實現上圖的布局

<TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/name" />
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        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="fill_parent"
        android:numeric="integer"
        android:id="@+id/age" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button"
        android:onClick="save"
        />

3.MainActivity的代碼如下

public class MainActivity extends Activity {
   private EditText  nameText;
   private EditText  ageText;
   private PreferencesService service;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        nameText = (EditText)this.findViewById(R.id.name);
        ageText = (EditText)this.findViewById(R.id.age);
        service = new PreferencesService(this);//每次點擊保存都會實例化該方法
    }
                                                                                                               
    public void save(View v) {
        String name = nameText.getText().toString();
        String age = ageText.getText().toString();
        //PreferencesService service = new PreferencesService(this);//每次點擊保存都會實例化,放到啟動方法中
        service.save(name,Integer.valueOf(age));
        Toast.makeText(getApplicationContext(), R.string.success, 1).show();
    }
}

4.PreferencesService類的代碼如下


public class PreferencesService {
    private Context context;
    public PreferencesService(Context context) {//得到上下對象
        this.context = context;
    }
    /**
     * 保存參數
     * @param name
     * @param age
     */
    public void save(String name, Integer age) {
        //取得SharePreferences對象,通過上下文環(huán)境得到
        SharedPreferences preferences = context.getSharedPreferences("gao", Context.MODE_PRIVATE);
         Editor editor = preferences.edit();//得到編輯器對象
         editor.putString("name", name);
         editor.putInt("age", age);//到此數據保存在內存中
         editor.commit();//把內存中的數據提交到文件中
    }
}

運行結果產生的xml文件中的數據


SharedPreferences保存、顯示用戶偏好設置

   以上即實現了用戶自己對軟件偏好參數的保存,那么如何讀取用戶的偏好參數呢?如用戶打開上述軟件時,顯示用戶的參數設置,如下圖

SharedPreferences保存、顯示用戶偏好設置

   實現方法是,在PreferencesService類中添加getPreferences()方法,具體代碼如下

/**
     * 獲取各項配置參數
     * @return  參數值
     */
    public Map<String,String> getPreferences(){
        Map<String,String> params = new HashMap<String,String>();
        //取得SharePreferences對象,通過上下文環(huán)境得到,"gao"是之前保存好的數據名稱,注意不帶后綴名
        SharedPreferences preferences = context.getSharedPreferences("gao", Context.MODE_PRIVATE);
        params.put("name", preferences.getString("name", "you name"));
        params.put("age", String.valueOf(preferences.getInt("age", 0)));
        return params;
    }

   在MainActivity類的OnCreate()方法中添加如下代碼

//第一次運行時顯示參數
        Map<String,String> params = service.getPreferences();
        nameText.setText(params.get("name"));
        ageText.setText(params.get("age"));

當再次執(zhí)行時,用戶輸入新的數據并點擊保存,那么就會保存最近的用戶輸入的數據。

注:附件文章中的代碼。

附件:http://down.51cto.com/data/2364229
向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI