保存复选框状态android xamarin [英] Save checkbox state android xamarin

查看:133
本文介绍了保存复选框状态android xamarin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是xamarin的新手,即使关闭了应用程序,我也试图保存我的复选框状态,因为当我关闭它时,复选框会重置为取消选中状态...

I am new on xamarin and i am trying to save my checkbox state even if the app is closed because when i close it the checkbox reset to uncheck state...

还..被更改的图像会重置..有什么办法可以同时保存这两个图像吗?

also.. the image that was changed resets.. is there any way to preserve both?

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

        SetContentView(Resource.Layout.layout1);

        var seletor = FindViewById<CheckBox>(Resource.Id.checkBox1);
        var imagem = FindViewById<ImageView>(Resource.Id.imageView1);

        seletor.Click += (o, e) => {
            if (seletor.Checked)
                imagem.SetImageResource(Resource.Drawable.estado1);

            else
                imagem.SetImageResource(Resource.Drawable.estado2);

        };
    }


推荐答案

您是否尝试过使用

检查以下内容:如何保存用户设置

将选择的选项保存为onclose或suspend ..并检索onResume / OnLoad

Store the option selected onclose or suspend.. and retrieve onResume / OnLoad

类似的东西:

 // Function called from OnDestroy
protected void saveset(){

    //store
    var prefs = Application.Context.GetSharedPreferences("MyApp", FileCreationMode.Private);
    var prefEditor = prefs.Edit();
    prefEditor.PutString("PrefName", "Some value");
    prefEditor.Commit();

}

// Function called from OnCreate
protected void retrieveset()
{
    //retreive 
    var prefs = Application.Context.GetSharedPreferences("MyApp", FileCreationMode.Private);              
    var somePref = prefs.GetString("PrefName", null);

    //Show a toast
    RunOnUiThread(() => Toast.MakeText(this, somePref, ToastLength.Long).Show());

}

如提供的链接。

当然,您需要适应您的需求并获取/填充复选框的值。

Of course you'll need to adapt to your needs and get / populate the value of the checkbox.

如果需要,您可以也可以实现某种类型的数据库并使用相同的机制来持久化和检索设置。

If you want, you can also implement some kind of db and use the same mechanism to persist and retrieve settings.

通常这是我用来存储设置和持久化值的内容,记住。

This is usually what I use to store settings and persist values that I need to "remember"

这是一个示例,说明我如何在一个应用程序中使用相同的行为,而不是复选框。但是您可以看到它的工作原理。我删除了一些代码,但我认为应该是一个很好的例子。

This is an example of how I'm using the same behavior in one app.. not for a checkbox.. but you can see how it works. I removed some code, but I think should be a good example.

[Activity(Label = "@string/ApplicationName",
        Icon = "@drawable/Icon")]
    public class PersonalDetailsActivity : Activity
    {
        ...
        private ISharedPreferencesEditor prefEditor;
        private ISharedPreferences preferences;
        ...

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

            SetContentView(Resource.Layout.PersonalDetailView);

            preferences = Application.Context.GetSharedPreferences("AppName", FileCreationMode.Private);


            PopulatePersistedData();
        }

        private void PopulatePersistedData()
        {
            myId = preferences.GetInt(nameof(myData.Id), 0);

            name.Text = preferences.GetString(nameof(myData.Name), null);
            address.Text = preferences.GetString(nameof(myData.Address), null);
            city.Text = preferences.GetString(nameof(myData.City), null);
            county.Text = preferences.GetString(nameof(myData.County), null);
            emailAddress.Text = preferences.GetString(nameof(myData.Email), null);
            phoneNumber.Text = preferences.GetString(nameof(myData.PhoneNumber), null);
            bio.Text = preferences.GetString(nameof(myData.Bio), null);
            rating.Rating = 5;

        }

        private void SaveButton_Click(object sender, EventArgs e)
        {
            prefEditor = preferences.Edit();

            myData = new Citizen();

            myData.Name = name.Text;
            myData.Address = address.Text;
            myData.City = city.Text;
            myData.County = county.Text;
            myData.Email = emailAddress.Text;
            myData.PhoneNumber = phoneNumber.Text;
            myData.Bio = bio.Text;

            prefEditor.PutInt(nameof(myData.Id), myId);
            prefEditor.PutString(nameof(myData.Name), myData.Name);
            prefEditor.PutString(nameof(myData.Address), myData.Address);
            prefEditor.PutString(nameof(myData.City), myData.City);
            prefEditor.PutString(nameof(myData.County), myData.County);
            prefEditor.PutString(nameof(myData.Email), myData.Email);
            prefEditor.PutString(nameof(myData.PhoneNumber), myData.PhoneNumber);
            prefEditor.PutString(nameof(myData.Bio), myData.Bio);

            prefEditor.Apply();
            prefEditor.Commit();

            var intent = new Intent();
            intent.PutExtra("CitizenName", name.Text);

            SetResult(Result.Ok, intent);
            this.Finish();
        }
    }

这篇关于保存复选框状态android xamarin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆