Using Shared Preferences in Android

前端 未结 5 1119
别跟我提以往
别跟我提以往 2021-01-27 05:05

I have three activities, A, B & C. Where A is a splash Activity and B Contains Login screen which consist of user Id and Password Text Field and one button to login. When I

5条回答
  •  感情败类
    2021-01-27 05:39

    to use shared preference in android

    public class SharedPref {
    
    public static void setValue(String key, String value, Context context) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString(key, value);
        editor.commit();
    }
    
    public static String getValue(String key, Context context) {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        return preferences.getString(key, null);
    }
     public static void setAlertDialog(Context mContext,String title,String message)
    {
        AlertDialog alertDialog = new AlertDialog.Builder(mContext).create();
        alertDialog.setTitle(title);
        alertDialog.setMessage(message);
        alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
    
                        dialog.dismiss();
                    }
                });
        alertDialog.show();
    }
    
    }
    

    and to set and get value from the class use following code

    SharedPref.setConfig("key","value",Context);
    SharedPref.getConfig("key",Context);
    SharedPref.setAlertDialog(Context,"title","Content to print");
    

提交回复
热议问题