保存复选框状态android xamarin

时间:2017-09-29 06:36:24

标签: c# android xamarin checkbox activity-lifecycle

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

也..已更改的图像重置..有没有办法保留两者?

 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);

        };
    }

1 个答案:

答案 0 :(得分:0)

您是否尝试过使用偏好设置?

检查以下内容:How to save user settings

存储选中的选项onclose或suspend ..并检索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());

}

与所提供的链接一样。

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

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

这通常是我用来存储设置和持久存储我需要“记住”

的值

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

[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();
        }
    }