已点击评论的按钮将不会在用户下次打开应用时显示
A button with comment having already clicked will not show up the next time user opens app
我希望用户下次打开应用时不再显示已点击评论的按钮。我在 google 上搜索并了解我应该使用共享首选项,但我不知道如何在应用程序中使用共享首选项。
我想知道如何使用 sharedpreferences 作为可见性按钮?
这就是您使用共享首选项的方式:
public class AppPrefrances {
protected static AppPrefrances INSTANCE;
private static SharedPreferences prefs;
public static AppPrefrances getInstance(Context context) {
if (INSTANCE == null) {
INSTANCE = new AppPrefrances();
prefs = PreferenceManager.getDefaultSharedPreferences(context);
}
return INSTANCE;
}
public void setClicked(String c) {
//click should be unique
prefs.edit().putString("click", c).apply();
}
public String getClicked() {
// 0 is the default value
return prefs.getString("click", "0");
}
}
然后从 activity 里面:
Button comment = (Button) findViewById(R.id.button);
if(AppPrefrances.getInstance(getApplicationContext()).getClicked().equals("1"))
{
comment.setVisibility(View.INVISIBLE);
}
comment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AppPrefrances.getInstance(getApplicationContext()).setClicked("1");
}
});
如果从应用的信息中选择了清除数据,共享偏好将被删除
我希望用户下次打开应用时不再显示已点击评论的按钮。我在 google 上搜索并了解我应该使用共享首选项,但我不知道如何在应用程序中使用共享首选项。
我想知道如何使用 sharedpreferences 作为可见性按钮?
这就是您使用共享首选项的方式:
public class AppPrefrances {
protected static AppPrefrances INSTANCE;
private static SharedPreferences prefs;
public static AppPrefrances getInstance(Context context) {
if (INSTANCE == null) {
INSTANCE = new AppPrefrances();
prefs = PreferenceManager.getDefaultSharedPreferences(context);
}
return INSTANCE;
}
public void setClicked(String c) {
//click should be unique
prefs.edit().putString("click", c).apply();
}
public String getClicked() {
// 0 is the default value
return prefs.getString("click", "0");
}
}
然后从 activity 里面:
Button comment = (Button) findViewById(R.id.button);
if(AppPrefrances.getInstance(getApplicationContext()).getClicked().equals("1"))
{
comment.setVisibility(View.INVISIBLE);
}
comment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AppPrefrances.getInstance(getApplicationContext()).setClicked("1");
}
});
如果从应用的信息中选择了清除数据,共享偏好将被删除