【问题标题】:Is there anyway to use switchCompat without resetting while running app in java, Android?无论如何在java,Android中运行应用程序时使用switchCompat而不重置?
【发布时间】:2021-02-23 06:35:45
【问题描述】:

我正在使用秒表应用程序。 我在主布局上有不同背景的 Switchcompat。 当我在运行时间时点击开关时,它会重置为 00:00:00。

有什么方法可以在不重置计时器的情况下使用开关?

以下是部分代码。

MainActivity

    SwitchCompat switchCompat;
    SharedPreferences sharedPreferences = null;

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

        switchCompat = findViewById(R.id.switchCompat);
        sharedPreferences = getSharedPreferences("night", 0);
        Boolean booleanValue = sharedPreferences.getBoolean("NightMode", false);
        if (booleanValue) {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
            switchCompat.setChecked(true);
        }

        switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                    switchCompat.setChecked(true);
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putBoolean("NightMode", true);
                    editor.commit();
                }else {
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                    switchCompat.setChecked(false);
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putBoolean("NightMode", false);
                    editor.commit();
                }
            }
        });
    }

【问题讨论】:

    标签: java android switchcompat


    【解决方案1】:

    签出saving instance state pattern - 存储时间值并在Activity 重新创建时重新存储

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);
        savedInstanceState.putString("SwitchText", switchCompat.getText().toString());
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        switchCompat = findViewById(R.id.switchCompat);
        if (savedInstanceState != null) { // is null on first start
            String switchText = savedInstanceState.getString("SwitchText");
            // may be null when no value stored under given key
            if (switchText != null) switchCompat.setText(switchText);
        }
        ...
    

    Activity 被重新创建/重新启动,因为在 onCheckedChanged 中您正在调用 AppCompatDelegate.setDefaultNightMode - 这迫使 Activity 重新启动并应用新主题,并且您必须编写一些额外的代码来保存以前的状态

    【讨论】:

      猜你喜欢
      • 2013-10-17
      • 1970-01-01
      • 2016-04-12
      • 2019-09-23
      • 1970-01-01
      • 2018-04-30
      • 1970-01-01
      • 2023-03-22
      • 2015-03-02
      相关资源
      最近更新 更多