Questions about Switch Shared Preference.

Asked 2 years ago, Updated 2 years ago, 83 views

            Switch _Gain_Switch = (Switch)_Adjust_Popup.FindViewById(Resource.Id._Gain_Switch);
            SeekBar _Gain_SeekBar = (SeekBar)_Adjust_Popup.FindViewById(Resource.Id._Gain_SeekBar);
            TextView _Gain_SeekBar_Text = (TextView)_Adjust_Popup.FindViewById(Resource.Id._Gain_SeekBar_Text);


            _Gain_SeekBar.Enabled = _Saved_Data.GetBoolean("_Test_SSS", true);
            _Gain_Switch.Checked = _Saved_Data.GetBoolean("_Test_SSS", true);
            _Gain_SeekBar.Progress = _Saved_Data.GetInt("_Test_Pro", 0);
            _Gain_SeekBar_Text.Text = "" + _Saved_Data.GetInt("_Test_Pro", 0);

            _Gain_Switch.CheckedChange += (a, b) =>
            {
                if (b.IsChecked == true)
                {
                    _Gain_SeekBar.Enabled = true;

                    _Data_Edit.PutBoolean("_Test_SSS", true);
                    _Data_Edit.Apply();

                    _Gain_SeekBar.Progress = _Saved_Data.GetInt("_Test_Pro", 0);
                    _Gain_SeekBar_Text.Text = "" + _Saved_Data.GetInt("_Test_Pro", 0);
                    _Gain_SeekBar.ProgressChanged += (c, d) =>
                    {
                        _Gain_SeekBar_Text.Text = string.Format("" + d.Progress);

                        _Data_Edit.PutInt("_Test_Pro", d.Progress);
                        _Data_Edit.Apply();
                    };
                }

                else if (b.IsChecked == false)
                {
                    Toast.MakeText (this, "You chose Auto", ToastLength.Short).Show();
                    _Gain_SeekBar.Enabled = false;
                    _Gain_SeekBar_Text.Enabled = false;

                    _Data_Edit.PutBoolean("_Test_SSS", false);
                    _Data_Edit.Apply();
                }
            };

This is the current situation. The question I want to ask is, I understand that you save and load the current state of the switch Is there a way to save and load data inside the switch at once?

For example, If Switch = true state is saved to Shared and true is retrieved even when re-run, Is there a way to bring up the textView modification or SeekBar expression that is executed within true?

You said you didn't understand, so I'll add the content.

Currently outside the Switch statement

            if (_Saved_Data.GetBoolean("_Test_SSS", true) == true)
            {
                _Gain_SeekBar.Progress = _Saved_Data.GetInt("_Test_Pro", 0);
                _Gain_SeekBar_Text.Text = "" + _Saved_Data.GetInt("_Test_Pro", 0);
            }
            else if(_Saved_Data.GetBoolean("_Test_SSS", true) == false)
            {
                _Gain_SeekBar.Enabled = false;
                _Gain_SeekBar_Text.Enabled = false;
            }

This sentence was added to determine the event when True was saved and False was saved, respectively.

To be more specific,

Like the sentence above

If the switch's true state is stored in the key value of "_Test_SSS", When rebooted, the switch remains in the true state, right? However, there is no action at all for TextViews that should be displayed when in the true state.

I don't know how to explain this any more ㅠ<

switch sharedpreferences

2022-09-22 19:42

1 Answers

The phenomenon that you don't see when you disable Seekbar seems to be invisible because the concept is different for each app theme, but it's probably disabled in light gray. When I tested it, it didn't show well when it was progress 0. I think simplifying the code is more about trying than seeking help. Everyone has a different coding style. What's the answer? It's hard to make a conclusion.

I made it with Java. Please refer to the flow.

public class TestLayoutActivity extends AppCompatActivity {
    private final static String KEY_SEEKBAR_VALUE = "seekbar_value";
    private final static String KEY_SWITCH_VALUE = "switch_value";

    private SeekBar mSeekBar;
    private Switch mSwitch;
    private TextView mTextView;

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

        mSeekBar = findViewById(R.id.seekBar);
        mSwitch = findViewById(R.id.testSwitch);
        mTextView = findViewById(R.id.textView);

        final boolean checkedVlaue = getSwitchValue();
        mSwitch.setChecked(checkedVlaue);
        updateVisibleState(checkedVlaue);

        final int progressValue = getSeekbarValue();
        mSeekBar.setProgress(progressValue);
        mTextView.setText(String.valueOf(progressValue));


        mSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                updateVisibleState(isChecked);
            }
        });

        mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                mTextView.setText(String.valueOf(progress));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

    private void updateVisibleState(boolean isChecked) {
        mSeekBar.setEnabled(isChecked);
        mTextView.setVisibility(isChecked ? View.VISIBLE : View.GONE);
    }

    @Override
    protected void onPause() {
        super.onPause();
        saveSeekbarValue(mSeekBar.getProgress());
        saveSwitchValue(mSwitch.isChecked());
    }

    private void saveSeekbarValue(int value) {
        final SharedPreferences.Editor prefs = PreferenceManager.getDefaultSharedPreferences(this).edit();
        prefs.putInt(KEY_SEEKBAR_VALUE, value);
        prefs.apply();
    }

    private int getSeekbarValue() {
        return PreferenceManager.getDefaultSharedPreferences(this).getInt(KEY_SEEKBAR_VALUE, 0);
    }

    private void saveSwitchValue(boolean value) {
        final SharedPreferences.Editor prefs = PreferenceManager.getDefaultSharedPreferences(this).edit();
        prefs.putBoolean(KEY_SWITCH_VALUE, value);
        prefs.apply();
    }

    private boolean getSwitchValue() {
        return PreferenceManager.getDefaultSharedPreferences(this).getBoolean(KEY_SWITCH_VALUE, false);
    }
}


2022-09-22 19:42

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.