When you save the Switch state in Shared Preference,

Asked 2 years ago, Updated 2 years ago, 139 views

This code is an Xamarin code.

        _Display_Switch = (Switch)_TT.FindViewById(Resource.Id._Display_Switch);
        _Display_Switch.Checked = _Saved_Data.GetBoolean("Test_Switch", false);
        if (_Saved_Data.GetBoolean("Test_Switch", false) == true)
        {
            Toast.MakeText (Activity, "Enable Display", ToastLength.Short).Show();
        }
        else if (_Saved_Data.GetBoolean("Test_Switch", false) == false)
        {
            Toast.MakeText (Activity, "Disable Display", ToastLength.Short).Show();
        }
        _Display_Switch.CheckedChange += _Display_Switch_CheckedChange; 



    private void _Display_Switch_CheckedChange(object sender, CompoundButton.CheckedChangeEventArgs e)
    {
        if (e.IsChecked == true)
        {
            Toast.MakeText (Activity, "Enable Display", ToastLength.Short).Show();
        }
        else if (e.IsChecked == false)
        {
            Toast.MakeText (Activity, "Disable Display", ToastLength.Short).Show();
        }

        _Data_Edit.PutBoolean("Test_Switch", e.IsChecked);
        _Data_Edit.Apply();
    }

You have to do the event from the top one more time. Even when you turn it off and on An event appeared. Can't I use this right away without declaring the event again in the sentence above?

        if (_Saved_Data.GetBoolean("Test_Switch", false) == true)
        {
            Toast.MakeText (Activity, "Enable Display", ToastLength.Short).Show();
        }
        else if (_Saved_Data.GetBoolean("Test_Switch", false) == false)
        {
            Toast.MakeText (Activity, "Disable Display", ToastLength.Short).Show();
        }

In other words, Even if I delete this part, I want the event to occur normally when it is turned off.

sharedpreferences switch

2022-09-22 18:03

1 Answers

After importing the switch object and registering CheckedChange, you can call the preference value and set the checked value. In the current order, it seems that the preference value is set first in checked and the checked change is registered.


2022-09-22 18:03

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.