如何在按钮上创建一个计时器并保存在共享电流(Android)中

发布于 2025-01-28 19:37:10 字数 438 浏览 2 评论 0原文

我想创建一个按钮,单击此按钮将在1分钟内无法单击,但是在重新启动程序后,此按钮是可单击的,因为我不明白 - 如何将其保存在共享prefrecterences中,

btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        btn.setEnabled(false);

     new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
                btn.setEnabled(true);

        }
    },120000);
    }
});

但是我需要如何将其保存在共享播放?

I want to create a button that after clicking this button will be not clickable for 1 min, but after restarting a program, this button is clickable cuz i didn't understand - how to save this in sharedpreferences

btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        btn.setEnabled(false);

     new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
                btn.setEnabled(true);

        }
    },120000);
    }
});

But how I need to save that in sharedpreferences?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沩ん囻菔务 2025-02-04 19:37:10

如果您想偏爱保存状态,则可以像以下

public void write(String key, boolean value) {
    SharedPreferences.Editor editor = 
    getSharedPreferences("Some Name",MODE_PRIVATE).edit();
    editor.putBoolean(key,value);
    editor.commit();
}

public Boolean read(String key, int defValue) {
    return getSharedPreferences("Some Name",MODE_PRIVATE).getBoolean(key, defValue);
}

单击按钮一样执行此操作。该值完成后,值并将值保存为false。

If you want to save your state in preference then you can do it like below

public void write(String key, boolean value) {
    SharedPreferences.Editor editor = 
    getSharedPreferences("Some Name",MODE_PRIVATE).edit();
    editor.putBoolean(key,value);
    editor.commit();
}

public Boolean read(String key, int defValue) {
    return getSharedPreferences("Some Name",MODE_PRIVATE).getBoolean(key, defValue);
}

On click of the button you can write your value to shared preference and when you are rendering your button you can get the value from preference and enable and disable it based on the value and also save the value as false when your time is finished.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文