如何使按钮不活动并将其保存在共享的首选项中

发布于 2025-01-27 19:18:21 字数 303 浏览 1 评论 0原文

重新启动后,如何使SharedPreferences中的按钮不活动CUZ处于活动状态?这是创建不活动按钮的代码

button.setOnClickListener(new View.OnClickListener() {            
                @Override
                public void onClick(View v) {
                    v.setClickable(false);
                }
            });

How to make the button inactive in sharedpreferences cuz after restart the button becomes active? here is the code to create an inactive button

button.setOnClickListener(new View.OnClickListener() {            
                @Override
                public void onClick(View v) {
                    v.setClickable(false);
                }
            });

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

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

发布评论

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

评论(1

九局 2025-02-03 19:18:21
public class MainActivity extends AppCompatActivity {
Button button;
// Initializing SharedPreferences
SharedPreferences sharedPreferences = getSharedPreferences("MySharedPref",MODE_PRIVATE);
SharedPreferences.Editor myEdit = sharedPreferences.edit();
static String  IS_DEACTIVE_KEY = "isActive";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button = findViewById(R.id.button);

    // Getting the isDeActive boolean from the sharedpreferences
    Boolean isButtonDeActive = sharedPreferences.getBoolean(IS_DEACTIVE_KEY, false);

    // Checking if we already set button deActive, if so, make it deActive
    if (isButtonDeActive){
        button.setClickable(false);
    }


    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            v.setClickable(false);
            // Saving DeActive state true in Sharedpreference
            myEdit.putBoolean(IS_DEACTIVE_KEY,true);
            myEdit.commit();
        }
    });
}
public class MainActivity extends AppCompatActivity {
Button button;
// Initializing SharedPreferences
SharedPreferences sharedPreferences = getSharedPreferences("MySharedPref",MODE_PRIVATE);
SharedPreferences.Editor myEdit = sharedPreferences.edit();
static String  IS_DEACTIVE_KEY = "isActive";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button = findViewById(R.id.button);

    // Getting the isDeActive boolean from the sharedpreferences
    Boolean isButtonDeActive = sharedPreferences.getBoolean(IS_DEACTIVE_KEY, false);

    // Checking if we already set button deActive, if so, make it deActive
    if (isButtonDeActive){
        button.setClickable(false);
    }


    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            v.setClickable(false);
            // Saving DeActive state true in Sharedpreference
            myEdit.putBoolean(IS_DEACTIVE_KEY,true);
            myEdit.commit();
        }
    });
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文