Android如何清除共享首选项中的数据?

时间:2014-08-22 05:59:17

标签: android eclipse

亲爱的朋友们,我想清除我曾尝试过的共享偏好

     SharedPreferences pref = getActivity().getSharedPreferences(
             "mypassword", getActivity().MODE_PRIVATE);
     Editor editor = pref.edit();

     editor.putString("key_username", "");  
     editor.putString("key_password", ""); 
     editor.commit(); 

但它没有用。我怎么能清楚请帮助我。

5 个答案:

答案 0 :(得分:2)

要清除共享首选项,您需要添加此代码。

SharedPreferences.Editor.clear();
SharedPreferences.Editor.commit();

答案 1 :(得分:1)

你尝试过使用

吗?
editor.apply();

取代

editor.commit();

答案 2 :(得分:1)

尝试以下代码。它将清除您的SharedPreferences。

     SharedPreferences pref = getActivity().getSharedPreferences(
             "mypassword", getActivity().MODE_PRIVATE);
     Editor editor = pref.edit();
     editor.clear(); 
     editor.commit();

答案 3 :(得分:1)

应该使用 -

SharedPreferences settings = view.getContext().getSharedPreferences("mypassword", 0);
SharedPreferences.Editor editor = settings.edit();
editor.clear();
editor.commit();

答案 4 :(得分:0)

尝试:

Editor e = pref.edit();
e.remove("key_username");
e.remove("key_password");
e.commit();
相关问题