【问题标题】:Android simple way to read / write dataAndroid 读取/写入数据的简单方法
【发布时间】:2016-09-23 09:23:32
【问题描述】:

所以我刚开始使用 JAVA,我一直在尝试将用户名和密码的简单组合保存到 SharedPreferences 中。虽然我不太确定我是否了解这实际上是如何工作的。所以这是我目前拥有的代码;

public void init() {
    EditText editText = (EditText) findViewById(R.id.password);

    editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            boolean handled = false;
            if (actionId == EditorInfo.IME_ACTION_SEND) {
                EditText passwordText = (EditText) findViewById(R.id.password);
                String Password =  passwordText.getText().toString();

                EditText usernameText = (EditText) findViewById(R.id.username);
                String Username = usernameText.getText().toString();

                SaveData(Username, Password);
                handled = true;
            }
            return handled;
        }
    });
}


private void SaveData(String Username, String Password) {

    SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPref.edit();
    editor.putString(getString(R.string.saved_high_score), newHighScore);
    editor.commit();
}

现在这当然行不通,因为我没有任何 getActivity(),但我想知道那应该是什么?或者我如何保存我发送给函数的已经可用的数据。

我的理想结果是能够从另一个活动中获取用户名和密码。现在我宁愿不使用本地文件,而是坚持使用 SharedPreferences。

提前非常感谢!

【问题讨论】:

标签: android sharedpreferences


【解决方案1】:

嘿,创建一个像下面这样的通用类,无论您想在共享首选项中存储值的任何位置,都可以使用它各自的功能来存储特定的数据类型。

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

    public class SharedPrefManager {

        public static SharedPreferences getSharedPref(Context mContext) {
            SharedPreferences pref = mContext.getSharedPreferences(Constants.SETTINGS, Context.MODE_PRIVATE);

            return pref;
        }

        public static void setPrefVal(Context mContext, String key, String value) {
            if(key!=null){
                SharedPreferences.Editor edit = getSharedPref(mContext).edit();
                edit.putString(key, value);
                edit.commit();
            }
        }

        public static void setIntPrefVal(Context mContext, String key, int value) {
            if(key!=null){
                SharedPreferences.Editor edit = getSharedPref(mContext).edit();
                edit.putInt(key, value);
                edit.commit();
            }
        }

        public static void setLongPrefVal(Context mContext, String key, Long value) {
            if(key!=null){
                SharedPreferences.Editor edit = getSharedPref(mContext).edit();
                edit.putLong(key, value);
                edit.commit();
            }
        }

        public static void setBooleanPrefVal(Context mContext, String key, boolean value) {
            if(key!=null){
                SharedPreferences.Editor edit = getSharedPref(mContext).edit();
                edit.putBoolean(key, value);
                edit.commit();
            }
        }


        public static String getPrefVal(Context mContext, String key) {
            SharedPreferences pref = getSharedPref(mContext);
            String val = "";
            try {
                if (pref.contains(key))
                    val = pref.getString(key, "");
                else
                    val = "";
            }catch (Exception e){
                e.printStackTrace();
            }
            return val;
        }

        public static int getIntPrefVal(Context mContext, String key) {
            SharedPreferences pref = getSharedPref(mContext);
            int val = 0;
            try {
                if(pref.contains(key)) val = pref.getInt(key, 0);
            }catch (Exception e){
                e.printStackTrace();
            }
            return val;
        }

        public static Long getLongPrefVal(Context mContext, String key) {
            SharedPreferences pref = getSharedPref(mContext);
            Long val = null;
            try{
                if(pref.contains(key)) val = pref.getLong(key, 0);
            }catch (Exception e){
                e.printStackTrace();
            }
            return val;
        }

        public static boolean getBooleanPrefVal(Context mContext, String key) {
            SharedPreferences pref = getSharedPref(mContext);
            boolean val = false;
            try{
                if(pref.contains(key)) val = pref.getBoolean(key, false);

            }catch (Exception e){
                e.printStackTrace();
            }
            return val;
        }
    }

如果有任何疑问,请询问。

【讨论】:

  • 完美无瑕,非常感谢,看起来也很干净!
  • 谢谢.. 如果它对你有帮助,我可以得到这个答案的支持和接受吗
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-24
  • 2012-01-04
  • 2015-02-14
  • 1970-01-01
相关资源
最近更新 更多