2017-09-29 191 views
1

我在xamarin新的,我想救我的複選框狀態,即使應用程序被關閉,因爲當我關閉它的複選框重置爲取消選中狀態......保存複選框狀態的Android 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); 

     }; 
    } 

回答

0

您是否嘗試過使用首選項?

檢查以下內容:How to save user settings

商店的OnClose選擇的選項或暫停..和檢索的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(); 
     } 
    } 
+0

我不知道如何修改這段代碼供我使用。 –

+0

基本上,創建prefs實例onLoad,當複選框更改時使用Prefs.Put,onResume,prefs.Get – rmjoia

+0

我看到..但我如何創建一個「onClose」或暫停? –