关闭应用程序后如何保存按钮的条件?
这是按钮的代码:
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
button.setEnabled(false);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
button.setEnabled(true);
}
},120000);
}
});
我知道我需要创建一个共享频率,但是我不知道如何保存具有计时器的按钮的条件,因为我知道如何在不使用处理程序的情况下保存。 我该怎么做?
This is code of button:
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
button.setEnabled(false);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
button.setEnabled(true);
}
},120000);
}
});
I know that i need to create a sharedpreferences, but I didn't understand how to save condition of button which have a timer, because i know how to save without ussing handler.
How can i do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须使用共享的偏好来保存状态。然后,在创建片段或活动的视图时,您应该检查使用共享偏好保存的值,并相应地设置按钮状态。
请允许我在Kotlin回答,也许您或您的IDE可以进行翻译。
要保存您的按钮状态以共享偏好,请定义此功能;
然后,要从共享的偏好中获取保存的按钮状态,请定义此功能;
在您的 oncreateview 根据您的保存值设置按钮状态。
然后在共享的偏好中切换保存的状态:
我希望这会有所帮助。 :)
You'll have to use shared preference to save the state. Then, while creating the view of you fragment or activity, you should check the value you saved with shared preference and set the button state accordingly.
Permit me, please, to answer in Kotlin, maybe you or your IDE can do the translation.
To save your button state to shared preference, define this function;
Then to get your saved button state from shared preference, define this function;
In your onCreateView set the button state in accordance to your saved value.
Then toggle the saved state in shared preference like so:
I hope this helps. :)