从AlertDialog调用onSharedPreferenceChanged()方法

时间:2014-02-13 23:11:00

标签: android

我有一个警告对话框,其中有一个编辑文本框和一个密码框。 在按下Alert Dialog的正按钮时,我想将输入的数据保存到我的首选项login_prefs,xml。任何人都可以帮我调用onSharedPreferencesChanged方法,以便我可以保存我的值。 我尝试了很多方法,但我没能实现它。 也可以任何人帮我从同一个

中检索默认值

由于

代码:

public class LoginCredsHandlerDialog extends SherlockDialogFragment {
String Tag = getClass().getSimpleName(), idString, passString , idKey = "loginId", passKey = "pass";
View view;
EditText id, pass;
SharedPreferences loginPrefs;
OnSharedPreferenceChangeListener listen;
final Context context = getSherlockActivity();
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder loginMenu = new AlertDialog.Builder(getSherlockActivity());
    LayoutInflater inflate = getSherlockActivity().getLayoutInflater();
    view = inflate.inflate(R.layout.prefs_layout, null);
    loginMenu.setView(view);
    id = (EditText) view.findViewById(R.id.loginId);
    pass = (EditText) view.findViewById(R.id.loginPass);
    Log.i(Tag, "before prefreneces class");
    new SaveData();
    loginPrefs  = PreferenceManager.getDefaultSharedPreferences(getSherlockActivity());
    Log.i(Tag, "linked to prefrences class");
    Log.i(Tag, "Got String Value");
    loginMenu.setTitle("Login Prefrences Menu");
    loginMenu.setCancelable(false);
    loginMenu.setPositiveButton("Save", new OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            Log.i(Tag, "Save Key Pressed");
            new SaveData().onSharedPreferenceChanged(loginPrefs, idKey);
            Log.i(Tag, "Values Saved");
        }
    });
    loginMenu.setNegativeButton("Cancel", new OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            LoginCredsHandlerDialog.this.getDialog().cancel();
        }
    });
    return loginMenu.create();
}
public class SaveData extends SherlockPreferenceActivity implements OnSharedPreferenceChangeListener {

    SharedPreferences spf;
    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.log_prefs);
        loginPrefs = PreferenceManager.getDefaultSharedPreferences(getSherlockActivity());
        idString = loginPrefs.getString(idKey, "");
        passString = loginPrefs.getString(passKey, "");
        if(idString.equals("") || passString.equals("")){
            idString = id.toString();
            passString = pass.toString();
        }

        id.setText(idString);
        pass.setText(passString);
    }


    @SuppressWarnings("deprecation")
    @Override
    protected void onResume() {
        super.onResume();
        // Set up a listener whenever a key changes
        getPreferenceScreen().getSharedPreferences()
                .registerOnSharedPreferenceChangeListener(this);
    }

    @SuppressWarnings("deprecation")
    @Override
    protected void onPause() {
        super.onPause();
        // Unregister the listener whenever a key changes
        getPreferenceScreen().getSharedPreferences()
                .unregisterOnSharedPreferenceChangeListener(this);
    }


    @SuppressWarnings("deprecation")
    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        if(key==idKey){
            Log.i(Tag, "saver");
            Preference pref = findPreference(key);
            pref.setDefaultValue(sharedPreferences.getString(key, ""));
        }
    }
}

}

1 个答案:

答案 0 :(得分:1)

你没有调用onSharedPreferencesChanged()函数,当有人改变偏好时会调用回调函数。

当您提交登录首选项时,将调用onSharedPreferencesChanged()。

您可以像这样优先保存价值:

loginPrefs.edit().putString("login_prefs", VALUE).commit();