簡體   English   中英

如何使用共享首選項保存多個數據?

[英]How to save multiple data using shared preferences?

我正在制作一個應用程序,其中在此活動中,我試圖從用戶那里獲取數據並使用共享首選項保存它。但是單擊注冊按鈕時應用程序崩潰。 在此處輸入圖片說明

這是 SignUpActivity.java 文件 -

package com.example.abinas.myapplication;

import android.content.Context;
import android.content.SharedPreferences; 
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText; 
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class SignUpActivity extends AppCompatActivity {

private EditText e3;
private EditText e4;
private EditText e5;
private EditText e6;
private Button b2;
private RadioGroup rg;
private RadioButton rb;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sign_up);
    rg = (RadioGroup)findViewById(R.id.radioroup);
    e3 = (EditText)findViewById(R.id.editText3);
    e4 = (EditText)findViewById(R.id.editText4);
    e5 = (EditText)findViewById(R.id.editText5);
    e6 = (EditText)findViewById(R.id.editText6);
    b2 = (Button)findViewById(R.id.button2);
    int selected_id = rg.getCheckedRadioButtonId();
    rb = (RadioButton)findViewById(selected_id);
}

public void onButtonClick(View view){

    final SharedPreferences pref;
    pref = getSharedPreferences("user_info",Context.MODE_PRIVATE);
    SharedPreferences.Editor edit = pref.edit();
    edit.putString("gender",rb.getText().toString());
    edit.putString("username",e4.getText().toString());
    edit.putString("name",e3.getText().toString());
    edit.putString("password",e5.getText().toString());
    edit.putInt("phone",Integer.parseInt(e6.getText().toString()));
    edit.apply();
    edit.commit();
    //b2.setText("SAVED");
    //b2.setClickable(false);
}
}

應該做哪些更改,才能成功保存數據?

 1. Create a public class SharedPreference Manager and a class Keystring that will contain multiple data like name, user name , password, phone number.
 2. Then create object of this class anywhere in the project
 3. Get and put value easily anywhere

共享首選項管理器

    import android.content.Context;
    import android.content.SharedPreferences;
    import android.content.SharedPreferences.Editor;


public class SharedPreferenceManager {

    protected Context mContext;


    protected SharedPreferences mSettings;
    protected Editor mEditor;

    public SharedPreferenceManager(Context ctx, String prefFileName) {
        mContext = ctx;

        mSettings = mContext.getSharedPreferences(prefFileName,
                Context.MODE_PRIVATE);
        mEditor = mSettings.edit();
    }

    /***
     * Set a value for the key
     ****/
    public void setValue(String key, String value) {
        mEditor.putString(key, value);
        mEditor.commit();
    }


    /***
     * Set a value for the key
     ****/
    public void setValue(String key, int value) {
        mEditor.putInt(key, value);
        mEditor.commit();
    }

    /***
     * Set a value for the key
     ****/
    public void setValue(String key, double value) {
        setValue(key, Double.toString(value));
    }

    /***
     * Set a value for the key
     ****/
    public void setValue(String key, long value) {
        mEditor.putLong(key, value);
        mEditor.commit();
    }

    /****
     * Gets the value from the settings stored natively on the device.
     *
     * @param defaultValue Default value for the key, if one is not found.
     **/
    public String getValue(String key, String defaultValue) {
        return mSettings.getString(key, defaultValue);
    }

    public int getIntValue(String key, int defaultValue) {
        return mSettings.getInt(key, defaultValue);
    }

    public long getLongValue(String key, long defaultValue) {
        return mSettings.getLong(key, defaultValue);
    }

    /****
     * Gets the value from the preferences stored natively on the device.
     *
     * @param defValue Default value for the key, if one is not found.
     **/
    public boolean getValue(String key, boolean defValue) {
        return mSettings.getBoolean(key, defValue);
    }

    public void setValue(String key, boolean value) {
        mEditor.putBoolean(key, value);
        mEditor.commit();
    }



    /**
     * Clear all the preferences store in this {@link android.content.SharedPreferences.Editor}
     */
    public boolean clear() {
        try {
            mEditor.clear().commit();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * Removes preference entry for the given key.
     *
     * @param key
     */
    public void removeValue(String key) {
        if (mEditor != null) {
            mEditor.remove(key).commit();
        }
    }
}

鍵串

public class KeyString {
public static String PREFERENCE_NAME ="APP_PREF";
public static String NAME="NAME";
public static String USER_NAME="USER_NAME";
public static String PHONE_NUMBER="PHONE_NUMBER";
public static String PASSWORD="PASSWORD";
public static String USER_ID="USER_ID";

}

在項目任何地方的任何活動##

private SharedPreferenceManager preferenceManager=new new SharedPreferenceManager(context, KeyString.PREFERENCE_NAME);

現在設置一些值

preferenceManager.setValue(KeyString.PHONE_NUMBER,"01714262873");

從首選項獲取數據

String phoneNumber=preferenceManager.getValue(KeyString.PHONE_NUMBER,""); //default value ""

通過這種方法,您可以設置和獲取所需數量的變量和值。 只需添加 keyString 和 play.happy 編碼..

您的代碼同時具有edit.apply()edit.commit() 僅使用其中之一。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM