1

我想清除存储在共享首选项中的值,我正在使用此代码。

/*  SharedPreferences myPrefs = this.getSharedPreferences("myPrefs",
            Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = myPrefs.edit();
            editor.clear();
            editor.commit();   */

但得到这个错误。

The method getSharedPreferences(String, int) is undefined for the type new View.OnClickListener(){}

private OnClickListener logoBarListener = new OnClickListener() { /* * (non-Javadoc) * * @see android.view.View.OnClickListener#onClick(android.view.View) */ public void onClick(View v) {

        if (v.getId() == R.id.img_bottom_home) {

            showProgressBar(MainScreen.class);

        } else if (v.getId() == R.id.img_bottom_basket) {

            showProgressBar(ShopBasketGet.class);

        } else if (v.getId() == R.id.img_bottom_notification) {

            showProgressBar(Notification.class);

        } else if (v.getId() == R.id.img_bottom_login) {
            SharedPreferences myPrefs = getSharedPreferences("myPrefs",
                    MODE_WORLD_READABLE);
            SharedPreferences.Editor editor = myPrefs.edit();
            editor.clear();
            editor.commit();   

            showProgressBar();
        }
    }
};
4

8 回答 8

1

去掉this关键字(改成this.getSharedPreferences().getSharedPreferences()this的是内部类View.onClickListener(),而方法实际上是在Activity类中。

于 2012-05-31T10:09:34.590 回答
0

试试这个方法

 SharedPreferences pref = YourActivityName.this.getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
 pref.edit().clear().commit();

我认为您尝试清除 onclick 事件而不是使用这种方式

SharedPreferences myPrefs = v.getContext().getSharedPreferences("myPrefs",Context.MODE_PRIVATE)
SharedPreferences.Editor editor = myPrefs.edit();
editor.clear();
editor.commit();   
于 2012-05-31T10:25:04.570 回答
0

使用以下代码片段

    Context context=YourActivityName.this;
    SharedPreferences myPrefs = context.getSharedPreferences("myPrefs",
    Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = myPrefs.edit();
    editor.clear();
    editor.commit();  

希望这可以帮助,

维普尔

于 2012-05-31T10:25:57.577 回答
0

通过单击按钮调用 clearSharedPreferences() 方法并将类变量上下文传递给此方法

public class MyLinearLayout extends LinearLayout {
    SharedPreferences preferences;
    SharedPreferences.Editor editor;
    Context context;
    public MyLinearLayout(Context context) {
        super(context);
            this.context = context;
    }

    public MyLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    private void clearSharedPreferences(Context context) {
        preferences = context.getSharedPreferences("myPrefs",
                context.MODE_PRIVATE);
        editor = preferences.edit();
        editor.clear();
        editor.commit();
    }
}

//你的代码会变成

if (v.getId() == R.id.img_bottom_home) {

            showProgressBar(MainScreen.class);

        } else if (v.getId() == R.id.img_bottom_basket) {

            showProgressBar(ShopBasketGet.class);

        } else if (v.getId() == R.id.img_bottom_notification) {

            showProgressBar(Notification.class);

        } else if (v.getId() == R.id.img_bottom_login) {
            clearSharedPreferences(context);
            showProgressBar();
        }
    }
};
于 2012-05-31T10:50:16.350 回答
0

如果您在 Activity 类中创建 LinearLayout 类的对象,那么您应该在构造函数中传递 Context。

下面是狙击手

活动课

public class SampleActivity extends Activity
{
    public void onCreate(Bundle bundle)
     {
         MyLinearLayout layout = new MyLinearLayout(this); 
         -----
         -----
     }  
}

MyLinearLayout 类

public class MyLinearLayout extends LinearLayout {
    private Context context;
    public MyLinearLayout(Context context) {
        super(context);
        this.context=context; 
        SharedPreferences preferences=context.getSharedPreference("pref",
                context.MODE_PRIVATE);

    }

}

于 2012-05-31T10:58:25.673 回答
0
Editor editor = getSharedPreferences("sharedPreferenceName", Context.MODE_PRIVATE).edit();
editor.clear();
editor.commit();
于 2014-07-07T11:33:30.700 回答
0

删除 sharedPreference、一个 var 或所有 sharedPreference 的示例...

SharedPreferences sharedPreferences = getSharedPreferences("mysharedpreferences",MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();
// a specific var of sharedPreference....
//Mark in the editor that a preference value should be removed, which will be done in the actual preferences once commit() is called.
editor.remove("mySharedVar");

//if you want remove all sharedPreference...
//Mark in the editor to remove all values from the preferences. Once commit is called, the only remaining preferences will be any that you have defined in this editor.
editor.clear();

editor.apply();
于 2016-09-05T17:48:47.170 回答
0

你可以试试这堆代码。这些线条对我有用。希望它也对你有用。^_^

SharedPreferences preferences = getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();

editor.clear();
editor.apply();
于 2019-05-05T09:42:09.313 回答