如何使用SharedPreferences在Android Xamarin中保存我的复选框状态

时间:2017-10-02 01:28:37

标签: c# android checkbox xamarin.android sharedpreferences

我有两个活动,MainActivity和Pagina_1Activity。

在MainActivity中,我有一个使用Intent转到Pagina_1Activity的按钮。 在Pagina_1Activity中我有一个复选框和一个imageView。当我选中复选框时,它会更改imageView中的图像,并且效果很好。

我想要做的是使用SharedPreferences保存复选框状态,我试图使用整数值“int estado”来做到这一点。

我想要做的是:如果我回到MainActivity或关闭de app,它会保存复选框(如果已选中,则保持选中状态)。

任何人都可以帮助我,并告诉我为什么我的共享偏好不起作用?我想要几天......没有解决方案......

CheckBox checkbox;
    ImageView imageView;
    int estado;
    private ISharedPreferencesEditor editarEstado;
    private ISharedPreferences valorEstado;


    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        SetContentView(Resource.Layout.Pagina_1);

        //here i am trying to get the value of the estado for my checkbox state
        valorEstado = Application.Context.GetSharedPreferences("valorE", FileCreationMode.Private);
        var pegar = valorEstado.GetInt("Estado", 0);


        checkbox = FindViewById<CheckBox>(Resource.Id.checkBox1);
        imageView = FindViewById <ImageView>(Resource.Id.imageView1);

        checkbox.Click += delegate
        {
            if (checkbox.Checked)
            {
                estado = 1;
            }
            else
            {
                estado = 0;
            }

            if (estado == 1)
            {
                checkbox.Checked = true;
                imageView.SetImageResource(Resource.Drawable.guildwars_icone);
            }
            if (estado == 0)
            {
                checkbox.Checked = false;
                imageView.SetImageResource(Resource.Drawable.guardian);
            }

            //here i am trying to save the value of estado for my checkbox state
            valorEstado = Application.Context.GetSharedPreferences("valorE", FileCreationMode.Private);
            editarEstado = valorEstado.Edit();
            editarEstado.PutInt("Estado", estado);

            editarEstado.Commit();

        };         


    }

2 个答案:

答案 0 :(得分:0)

使用这种方式

保存bool值

ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences (this);
ISharedPreferencesEditor editor = prefs.Edit ();
editor.PutBoolean ("bool_value", mBool);
editor.Commit();  

要回复bool值

ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences (this);
mBool = prefs.GetBoolean ("bool_value", <default value>);

答案 1 :(得分:0)

@Ironman这里是代码。

 CheckBox checkbox;
    ImageView imageView;
    int estado = 0;
    private ISharedPreferencesEditor editor;
    private ISharedPreferences prefs;


    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        SetContentView(Resource.Layout.Pagina_1);

        //here i am trying to get the value of estado for my checkbox state
        ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(this);
        estado = prefs.GetInt("int_value", 0);

        checkbox = FindViewById<CheckBox>(Resource.Id.checkBox1);
        imageView = FindViewById <ImageView>(Resource.Id.imageView1);

        checkbox.Click += delegate
        {
            if (checkbox.Checked)
            {
                estado = 1;
            }
            else
            {
                estado = 0;
            }

            if (estado == 1)
            {
                checkbox.Checked = true;
                imageView.SetImageResource(Resource.Drawable.guildwars_icone);
            }
            if (estado == 0)
            {
                checkbox.Checked = false;
                imageView.SetImageResource(Resource.Drawable.guardian);
            }

            //THE ERRO CS0136 IS IN THIS prefs
            ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(this);
            ISharedPreferencesEditor editor = prefs.Edit();
            editor.PutInt("int_value", estado);
            editor.Commit();

        };




    }