在android应用程序中初始化一次变量然后使用并保存它以便进一步重用

时间:2015-01-13 16:34:13

标签: java android android-button

我的问题是这个 - 我有一个应用程序应该限制按钮点击,比如在应用程序的整个生命周期中最多5次点击。 在第6次点击某个按钮时,无论应用程序打开或关闭多少次,它都不应该读取OnClick事件。 伙计们我是一个普通的程序员,我足够了解android和java.Any帮助表示赞赏。 :)

1 个答案:

答案 0 :(得分:1)

您可以使用“共享首选项”存储值并检索它。

// Write Shared Preferences
SharedPreferences sharedPreferences = getSharedPreferences("MyPref", Context.MODE_PRIVATE);
SharedPreferences.Editor sharedPreferencesEditor = sharedPreferences.edit();
sharedPreferencesEditor.putInt("ClickCounter", counter);
sharedPreferencesEditor.apply();

// Read Shared Preferences
SharedPreferences sharedPreferences = getSharedPreferences("MyPref", Context.MODE_PRIVATE);
int clickCounter = sharedPreferences.getInt("ClickCounter", 0);