我有一个应用程序,我试图将按钮点击次数限制为五次,然后一旦用户按下此按钮五次,它应该禁用.
但是我收到了上述错误,我不知道为什么.
有任何想法吗 ?
buttonadd.setOnClickListener(new OnClickListener () {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), MainActivity3.class);
startActivity(intent);
int clicks = 0;
clicks++;
if (clicks >= 5){
buttonadd.setEnabled(false);
}
SharedPreferences prefs = Context.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt("clicks", clicks);
editor.apply();
}
});
Run Code Online (Sandbox Code Playgroud)