Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.09 KB | None | 0 0
  1. public void init() {
  2. EditText editText = (EditText) findViewById(R.id.password);
  3.  
  4. editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
  5. @Override
  6. public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
  7. boolean handled = false;
  8. if (actionId == EditorInfo.IME_ACTION_SEND) {
  9. EditText passwordText = (EditText) findViewById(R.id.password);
  10. String Password = passwordText.getText().toString();
  11.  
  12. EditText usernameText = (EditText) findViewById(R.id.username);
  13. String Username = usernameText.getText().toString();
  14.  
  15. SaveData(Username, Password);
  16. handled = true;
  17. }
  18. return handled;
  19. }
  20. });
  21. }
  22.  
  23.  
  24. private void SaveData(String Username, String Password) {
  25.  
  26. SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
  27. SharedPreferences.Editor editor = sharedPref.edit();
  28. editor.putString(getString(R.string.saved_high_score), newHighScore);
  29. editor.commit();
  30. }
  31.  
  32. import android.content.Context;
  33. import android.content.SharedPreferences;
  34.  
  35. public class SharedPrefManager {
  36.  
  37. public static SharedPreferences getSharedPref(Context mContext) {
  38. SharedPreferences pref = mContext.getSharedPreferences(Constants.SETTINGS, Context.MODE_PRIVATE);
  39.  
  40. return pref;
  41. }
  42.  
  43. public static void setPrefVal(Context mContext, String key, String value) {
  44. if(key!=null){
  45. SharedPreferences.Editor edit = getSharedPref(mContext).edit();
  46. edit.putString(key, value);
  47. edit.commit();
  48. }
  49. }
  50.  
  51. public static void setIntPrefVal(Context mContext, String key, int value) {
  52. if(key!=null){
  53. SharedPreferences.Editor edit = getSharedPref(mContext).edit();
  54. edit.putInt(key, value);
  55. edit.commit();
  56. }
  57. }
  58.  
  59. public static void setLongPrefVal(Context mContext, String key, Long value) {
  60. if(key!=null){
  61. SharedPreferences.Editor edit = getSharedPref(mContext).edit();
  62. edit.putLong(key, value);
  63. edit.commit();
  64. }
  65. }
  66.  
  67. public static void setBooleanPrefVal(Context mContext, String key, boolean value) {
  68. if(key!=null){
  69. SharedPreferences.Editor edit = getSharedPref(mContext).edit();
  70. edit.putBoolean(key, value);
  71. edit.commit();
  72. }
  73. }
  74.  
  75.  
  76. public static String getPrefVal(Context mContext, String key) {
  77. SharedPreferences pref = getSharedPref(mContext);
  78. String val = "";
  79. try {
  80. if (pref.contains(key))
  81. val = pref.getString(key, "");
  82. else
  83. val = "";
  84. }catch (Exception e){
  85. e.printStackTrace();
  86. }
  87. return val;
  88. }
  89.  
  90. public static int getIntPrefVal(Context mContext, String key) {
  91. SharedPreferences pref = getSharedPref(mContext);
  92. int val = 0;
  93. try {
  94. if(pref.contains(key)) val = pref.getInt(key, 0);
  95. }catch (Exception e){
  96. e.printStackTrace();
  97. }
  98. return val;
  99. }
  100.  
  101. public static Long getLongPrefVal(Context mContext, String key) {
  102. SharedPreferences pref = getSharedPref(mContext);
  103. Long val = null;
  104. try{
  105. if(pref.contains(key)) val = pref.getLong(key, 0);
  106. }catch (Exception e){
  107. e.printStackTrace();
  108. }
  109. return val;
  110. }
  111.  
  112. public static boolean getBooleanPrefVal(Context mContext, String key) {
  113. SharedPreferences pref = getSharedPref(mContext);
  114. boolean val = false;
  115. try{
  116. if(pref.contains(key)) val = pref.getBoolean(key, false);
  117.  
  118. }catch (Exception e){
  119. e.printStackTrace();
  120. }
  121. return val;
  122. }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement