2011-07-23 67 views
3

我有一個複選框的應用程序,我如何保存它們?Android - 如何保存複選框的狀態

我的佈局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout android:id="@+id/list_linear" 
android:layout_width="fill_parent" android:layout_height="fill_parent" 
xmlns:android="http://schemas.android.com/apk/res/android"> 
    <ScrollView android:id="@+id/ScrollView01" 
android:layout_width="fill_parent" 
android:layout_height="match_parent"> 

<TableLayout 
android:id="@+id/tableLayout1" 
android:layout_height="match_parent" 
android:layout_width="wrap_content"> 
    <CheckBox android:textSize="30sp" android:background="@drawable/checkbox_background" android:button="@drawable/checkbox" android:text="Ontbijtkoek" android:id="@+id/checkBox1" android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox> 
    <CheckBox android:textSize="30sp" android:background="@drawable/checkbox_background" android:button="@drawable/checkbox" android:text="Koekrepen" android:id="@+id/checkBox70" android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox> 
    <CheckBox android:textSize="30sp" android:background="@drawable/checkbox_background" android:button="@drawable/checkbox" android:text="Beschuit" android:id="@+id/checkBox69" android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox> 
    <CheckBox android:textSize="30sp" android:background="@drawable/checkbox_background" android:button="@drawable/checkbox" android:text="Chocolade" android:id="@+id/checkBox68" android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox> 
    <CheckBox android:textSize="30sp" android:background="@drawable/checkbox_background" android:button="@drawable/checkbox" android:text="Koekjes" android:id="@+id/checkBox67" android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox> 
    <CheckBox android:textSize="30sp" android:background="@drawable/checkbox_background" android:button="@drawable/checkbox" android:text="Brood" android:id="@+id/checkBox66" android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox> 
    <CheckBox android:textSize="30sp" android:background="@drawable/checkbox_background" android:button="@drawable/checkbox" android:text="Hagelslag puur" android:id="@+id/checkBox65" android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox> 
    <CheckBox android:textSize="30sp" android:background="@drawable/checkbox_background" android:button="@drawable/checkbox" android:text="Hagelslag mix" android:id="@+id/checkBox64" android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox> 
    <CheckBox android:textSize="30sp" android:background="@drawable/checkbox_background" android:button="@drawable/checkbox" android:text="Pindakaas" android:id="@+id/checkBox63" android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox> 
    <CheckBox android:textSize="30sp" android:background="@drawable/checkbox_background" android:button="@drawable/checkbox" android:text="Pasta" android:id="@+id/checkBox62" android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox> 
    <CheckBox android:textSize="30sp" android:background="@drawable/checkbox_background" android:button="@drawable/checkbox" android:text="Jam" android:id="@+id/checkBox61" android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox> 
    <CheckBox android:textSize="30sp" android:background="@drawable/checkbox_background" android:button="@drawable/checkbox" android:text="Thee" android:id="@+id/checkBox60" android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox> 
    <CheckBox android:textSize="30sp" android:background="@drawable/checkbox_background" android:button="@drawable/checkbox" android:text="Boter" android:id="@+id/checkBox59" android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox> 
    <CheckBox android:textSize="30sp" android:background="@drawable/checkbox_background" android:button="@drawable/checkbox" android:text="Smeerkaas" android:id="@+id/checkBox58" android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox> 
    <CheckBox android:textSize="30sp" android:background="@drawable/checkbox_background" android:button="@drawable/checkbox" android:text="Suiker" android:id="@+id/checkBox57" android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox> 
    <CheckBox android:textSize="30sp" android:background="@drawable/checkbox_background" android:button="@drawable/checkbox" android:text="Groente" android:id="@+id/checkBox56" android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox> 
    <CheckBox android:textSize="30sp" android:background="@drawable/checkbox_background" android:button="@drawable/checkbox" android:text="Fruit" android:id="@+id/checkBox55" android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox> 
    <CheckBox android:textSize="30sp" android:background="@drawable/checkbox_background" android:button="@drawable/checkbox" android:text="Aardappeltjes" android:id="@+id/checkBox54" android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox> 
    <CheckBox android:textSize="30sp" android:background="@drawable/checkbox_background" android:button="@drawable/checkbox" android:text="Zoutjes" android:id="@+id/checkBox53" android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox> 
    <CheckBox android:textSize="30sp" android:background="@drawable/checkbox_background" android:button="@drawable/checkbox" android:text="Sapjes" android:id="@+id/checkBox52" android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox>  
</TableLayout> 
</ScrollView> 
</LinearLayout> 

(是的,我現在是多少複選框)

而且我main.class是:

package com.boodschappen; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.Dialog; 
import android.content.DialogInterface; 
import android.content.SharedPreferences; 
import android.graphics.Color; 
import android.graphics.Typeface; 
import android.os.Bundle; 
import android.preference.PreferenceManager; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.Window; 
import android.view.WindowManager; 
import android.widget.CheckBox; 
import android.widget.TextView; 

public class main extends Activity { 
    @Override 
    public void onBackPressed() { 

      showDialog(MENU_EXIT); 

      super.onBackPressed(); 
    } 

private static final int MENU_SAVE = 0; 
private static final int MENU_DIALOG = 2; 
private static final int MENU_EXIT = 3; 
private static final int MENU_LOAD = 1; 

CheckBox een, v52, v53, v54, v55, v56, v57, v58, v59, z60, z61, z62, z63, z64, z65, z66, z67, z68, z69, z70 ; 

@Override 
public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
      WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, 
      WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 
    setContentView(R.layout.list); 



    Typeface fontFace = Typeface.createFromAsset(getAssets(), "fonts/ankehand.ttf"); 

    TextView someText1 = (TextView)findViewById(R.id.checkBox1); 
    TextView someText52 = (TextView)findViewById(R.id.checkBox52); 
    TextView someText53 = (TextView)findViewById(R.id.checkBox53); 
    TextView someText54 = (TextView)findViewById(R.id.checkBox54); 
    TextView someText55 = (TextView)findViewById(R.id.checkBox55); 
    TextView someText56 = (TextView)findViewById(R.id.checkBox56); 
    TextView someText57 = (TextView)findViewById(R.id.checkBox57); 
    TextView someText58 = (TextView)findViewById(R.id.checkBox58); 
    TextView someText59 = (TextView)findViewById(R.id.checkBox59); 
    TextView someText60 = (TextView)findViewById(R.id.checkBox60); 
    TextView someText61 = (TextView)findViewById(R.id.checkBox61); 
    TextView someText62 = (TextView)findViewById(R.id.checkBox62); 
    TextView someText63 = (TextView)findViewById(R.id.checkBox63); 
    TextView someText64 = (TextView)findViewById(R.id.checkBox64); 
    TextView someText65 = (TextView)findViewById(R.id.checkBox65); 
    TextView someText66 = (TextView)findViewById(R.id.checkBox66); 
    TextView someText67 = (TextView)findViewById(R.id.checkBox67); 
    TextView someText68 = (TextView)findViewById(R.id.checkBox68); 
    TextView someText69 = (TextView)findViewById(R.id.checkBox69); 
    TextView someText70 = (TextView)findViewById(R.id.checkBox70); 

    someText1.setTypeface(fontFace); // only typeface 
    someText52.setTypeface(fontFace); // only typeface 
    someText53.setTypeface(fontFace); // only typeface 
    someText54.setTypeface(fontFace); // only typeface 
    someText55.setTypeface(fontFace); // only typeface 
    someText56.setTypeface(fontFace); // only typeface 
    someText57.setTypeface(fontFace); // only typeface 
    someText58.setTypeface(fontFace); // only typeface 
    someText59.setTypeface(fontFace); // only typeface 
    someText60.setTypeface(fontFace); // only typeface 
    someText61.setTypeface(fontFace); // only typeface 
    someText62.setTypeface(fontFace); // only typeface 
    someText63.setTypeface(fontFace); // only typeface 
    someText64.setTypeface(fontFace); // only typeface 
    someText65.setTypeface(fontFace); // only typeface 
    someText66.setTypeface(fontFace); // only typeface 
    someText67.setTypeface(fontFace); // only typeface 
    someText68.setTypeface(fontFace); // only typeface 
    someText69.setTypeface(fontFace); // only typeface 
    someText70.setTypeface(fontFace); // only typeface 



    een = (CheckBox) findViewById(R.id.checkBox1); 
    een.setOnClickListener(new OnClickListener(){ 
      public void onClick(View v){ 
       if(((CheckBox) v).isChecked()){ 
        een.setTextColor(R.color.black); 
       } else { 
        een.setTextColor(Color.rgb(255,255,255)); 
       } 
      } 
     }); 
    v42 = (CheckBox) findViewById(R.id.checkBox52); 
    v42.setOnClickListener(new OnClickListener(){ 
      public void onClick(View v){ 
       if(((CheckBox) v).isChecked()){ 
        v42.setTextColor(R.color.black); 
       } else { 
        v42.setTextColor(Color.rgb(255,255,255)); 
       } 
      } 
     }); 
    v43 = (CheckBox) findViewById(R.id.checkBox53); 
    v43.setOnClickListener(new OnClickListener(){ 
      public void onClick(View v){ 
       if(((CheckBox) v).isChecked()){ 
        v43.setTextColor(R.color.black); 
       } else { 
        v43.setTextColor(Color.rgb(255,255,255)); 
       } 
      } 
     }); 
    v44 = (CheckBox) findViewById(R.id.checkBox54); 
    v44.setOnClickListener(new OnClickListener(){ 
      public void onClick(View v){ 
       if(((CheckBox) v).isChecked()){ 
        v44.setTextColor(R.color.black); 
       } else { 
        v44.setTextColor(Color.rgb(255,255,255)); 
       } 
      } 
     }); 
    v45 = (CheckBox) findViewById(R.id.checkBox55); 
    v45.setOnClickListener(new OnClickListener(){ 
      public void onClick(View v){ 
       if(((CheckBox) v).isChecked()){ 
        v45.setTextColor(R.color.black); 
       } else { 
        v45.setTextColor(Color.rgb(255,255,255)); 
       } 
      } 
     }); 
    v46 = (CheckBox) findViewById(R.id.checkBox56); 
    v46.setOnClickListener(new OnClickListener(){ 
      public void onClick(View v){ 
       if(((CheckBox) v).isChecked()){ 
        v46.setTextColor(R.color.black); 
       } else { 
        v46.setTextColor(Color.rgb(255,255,255)); 
       } 
      } 
     }); 
    v47 = (CheckBox) findViewById(R.id.checkBox57); 
    v47.setOnClickListener(new OnClickListener(){ 
      public void onClick(View v){ 
       if(((CheckBox) v).isChecked()){ 
        v47.setTextColor(R.color.black); 
       } else { 
        v47.setTextColor(Color.rgb(255,255,255)); 
       } 
      } 
     }); 
    v48 = (CheckBox) findViewById(R.id.checkBox58); 
    v48.setOnClickListener(new OnClickListener(){ 
      public void onClick(View v){ 
       if(((CheckBox) v).isChecked()){ 
        v48.setTextColor(R.color.black); 
       } else { 
        v48.setTextColor(Color.rgb(255,255,255)); 
       } 
      } 
     }); 
    v49 = (CheckBox) findViewById(R.id.checkBox59); 
    v49.setOnClickListener(new OnClickListener(){ 
      public void onClick(View v){ 
       if(((CheckBox) v).isChecked()){ 
        v49.setTextColor(R.color.black); 
       } else { 
        v49.setTextColor(Color.rgb(255,255,255)); 
       } 
      } 
     }); 
    v50 = (CheckBox) findViewById(R.id.checkBox60); 
    v50.setOnClickListener(new OnClickListener(){ 
      public void onClick(View v){ 
       if(((CheckBox) v).isChecked()){ 
        v50.setTextColor(R.color.black); 
       } else { 
        v50.setTextColor(Color.rgb(255,255,255)); 
       } 
      } 
     }); 
    v51 = (CheckBox) findViewById(R.id.checkBox61); 
    v51.setOnClickListener(new OnClickListener(){ 
      public void onClick(View v){ 
       if(((CheckBox) v).isChecked()){ 
        v51.setTextColor(R.color.black); 
       } else { 
        v51.setTextColor(Color.rgb(255,255,255)); 
       } 
      } 
     }); 
    v52 = (CheckBox) findViewById(R.id.checkBox62); 
    v52.setOnClickListener(new OnClickListener(){ 
      public void onClick(View v){ 
       if(((CheckBox) v).isChecked()){ 
        v52.setTextColor(R.color.black); 
       } else { 
        v52.setTextColor(Color.rgb(255,255,255)); 
       } 
      } 
     }); 
    v53 = (CheckBox) findViewById(R.id.checkBox63); 
    v53.setOnClickListener(new OnClickListener(){ 
      public void onClick(View v){ 
       if(((CheckBox) v).isChecked()){ 
        v53.setTextColor(R.color.black); 
       } else { 
        v53.setTextColor(Color.rgb(255,255,255)); 
       } 
      } 
     }); 
    v54 = (CheckBox) findViewById(R.id.checkBox64); 
    v54.setOnClickListener(new OnClickListener(){ 
      public void onClick(View v){ 
       if(((CheckBox) v).isChecked()){ 
        v54.setTextColor(R.color.black); 
       } else { 
        v54.setTextColor(Color.rgb(255,255,255)); 
       } 
      } 
     }); 
    v55 = (CheckBox) findViewById(R.id.checkBox65); 
    v55.setOnClickListener(new OnClickListener(){ 
      public void onClick(View v){ 
       if(((CheckBox) v).isChecked()){ 
        v55.setTextColor(R.color.black); 
       } else { 
        v55.setTextColor(Color.rgb(255,255,255)); 
       } 
      } 
     }); 
    v56 = (CheckBox) findViewById(R.id.checkBox66); 
    v56.setOnClickListener(new OnClickListener(){ 
      public void onClick(View v){ 
       if(((CheckBox) v).isChecked()){ 
        v56.setTextColor(R.color.black); 
       } else { 
        v56.setTextColor(Color.rgb(255,255,255)); 
       } 
      } 
     }); 
    v57 = (CheckBox) findViewById(R.id.checkBox67); 
    v57.setOnClickListener(new OnClickListener(){ 
      public void onClick(View v){ 
       if(((CheckBox) v).isChecked()){ 
        v57.setTextColor(R.color.black); 
       } else { 
        v57.setTextColor(Color.rgb(255,255,255)); 
       } 
      } 
     }); 
    v58 = (CheckBox) findViewById(R.id.checkBox68); 
    v58.setOnClickListener(new OnClickListener(){ 
      public void onClick(View v){ 
       if(((CheckBox) v).isChecked()){ 
        v58.setTextColor(R.color.black); 
       } else { 
        v58.setTextColor(Color.rgb(255,255,255)); 
       } 
      } 
     }); 
    v59 = (CheckBox) findViewById(R.id.checkBox69); 
    v59.setOnClickListener(new OnClickListener(){ 
      public void onClick(View v){ 
       if(((CheckBox) v).isChecked()){ 
        v59.setTextColor(R.color.black); 
       } else { 
        v59.setTextColor(Color.rgb(255,255,255)); 
       } 
      } 
     }); 
    z60 = (CheckBox) findViewById(R.id.checkBox70); 
    z60.setOnClickListener(new OnClickListener(){ 
      public void onClick(View v){ 
       if(((CheckBox) v).isChecked()){ 
        z60.setTextColor(R.color.black); 
       } else { 
        z60.setTextColor(Color.rgb(255,255,255)); 
       } 
      } 
     }); 
} 




@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    menu.add(0, MENU_SAVE, 0, "Opslaan"); 
    menu.add(1,MENU_LOAD, 1, "Laden"); 
    menu.add(2, MENU_DIALOG, 2, "About"); 
    menu.add(3, MENU_EXIT, 3, "Exit"); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
    case MENU_SAVE: 
     Here i want to Save the Checkboxes 
     return true; 
    case MENU_LOAD: 
     Here i want to Load the Checkboxes 
     return true; 
    case MENU_DIALOG: 
     Dialog dialog = new Dialog(lidl.this); 
     dialog.setContentView(R.layout.custom_dialog); 
     dialog.setTitle("About"); 
     dialog.setCancelable(true); 
     //there are a lot of settings, for dialog, check them all out! 

     //set up text 
     TextView text = (TextView) dialog.findViewById(R.id.text); 
     text.setText("Hallo, deze applicatie is een variant op Appie©. Deze applicatie is op volgorde van de winkel waar wij meestal onze boodschappen doen. Wilt u ook zo'n applicatie, ga dan naar het tabje Mail. Zet daarin welke winkel(s) het zijn, en wat u daar altijd koopt."); 

     dialog.show(); 
     return true; 
    case MENU_EXIT: 
     AlertDialog.Builder alt_bld = new AlertDialog.Builder(this); 
     alt_bld.setMessage("Weet je zeker dat je wilt afsluiten?") 
     .setCancelable(false) 
     .setPositiveButton("Ja", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int id) { 
     System.exit(0); 
     } 
     }) 
     .setNegativeButton("Nee", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int id) { 
     // Action for 'NO' Button 
     dialog.cancel(); 
     } 
     }); 
     AlertDialog alert = alt_bld.create(); 
     // Title for AlertDialog 
     alert.setTitle("Exit"); 
     alert.show(); 

    return true; 
    } 
    return false; 
} 
} 

你現在如何做到這一點?

我希望你能理解我,我希望你能幫助我。

Gaauwe

編輯:

這裏是我想要保存的東西和事物負載代碼:

@Override 
     public boolean onCreateOptionsMenu(Menu menu) { 
     menu.add(2, MENU_SAVE, 2, "Save"); 
     menu.add(3, MENU_LOAD, 3, "Load"); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
    case MENU_SAVE: 
     Here save the checkboxes 
     return true; 
    case MENU_LOAD: 
     Here load the checkboxes 

    return true; 

回答

15

如何SharedPreferences?您可以使用鍵值格式存儲和加載複選框的狀態和名稱。 How to use

E. g。是這樣的:

// Init 
SharedPreferences settings = getSharedPreferences("mysettings", 0); 
SharedPreferences.Editor editor = settings.edit(); 

// Save 
boolean checkBoxValue = v42.isChecked();   
     editor.putBoolean("v42", checkBoxValue); 
     editor.commit();; 

// Load 
v42.setChecked(settings.getBoolean("v42", false)); 

但是你應該實現更有效的方式,可能是通過所有的複選框循環讀取它的名稱或ID,您可以在離開活動之前調用。

+0

我,我必須使用sharedpreferences,但怎麼樣,那我現在不,你能也許張貼例子,或只是,我可以使用的代碼? – Gaauwe

+0

你能幫我嗎? – Gaauwe

+0

所以我有一個選項:案例MENU_WEEK: \t \t \t SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); \t \t \t SharedPreferences.Editor編輯= settings.edit(); \t \t \t布爾checkBoxValue = v42.isChecked(); \t editor.putBoolean(「v42」,checkBoxValue); \t editor.commit();; \t返回true; \t \t case MENU_LOAD: \t \t \t v42.setChecked()= settings.getBoolean(「v42」,false); \t返回true;但後來它說:賦值的左側必須是變量 – Gaauwe

2

重寫onSaveInstanceState(Bundle outState)並將哪些選項寫入outState。然後,你可以看到,當活動被從savedInstanceState提取這些信息來創造傳遞到的onCreate(捆綁savedInstanceState)

例如哪些檢查,

@Override 
protected void onSaveInstanceStat(Bundle b){ 
    b.putBoolean("een",een.isChecked()); 
    b.putBoolean("v52",v52.isChecked()); 
    b.putBoolean("v53",v53.isChecked()); 

    // ... etc 
} 

@Override 
protected void onCreate (Bundle savedInstanceState){ 
    // everything you have in your onCreate implementation 

    een.setChecked(b.getBoolean("een")); 
    v52.setChecked(b.getBoolean("v52")); 
    v53.setChecked(b.getBoolean("v53")); 

    // ... etc 
} 

重要:沒有保證「的onSaveInstanceState」將被調用。因此,只有在保存UI狀態時才使用它,這是爲了方便用戶而不是對功能至關重要。如果你沒有在他們的設備上保存數據,例如使用SharedPreference,用戶會更高興。

+0

現在,我必須使用sharedpreferences,但我怎麼了,我是這麼認爲現在不,你能也許張貼例子,或只是代碼,我可以使用 – Gaauwe

+0

是新到Android,所以也許你能告訴我怎麼樣?我可以在選項菜單中設置此,保存和加載選項菜單,我在我的描述寫 – Gaauwe

+0

你停止與幫助我:??'( – Gaauwe

1

如果您想在會話中保存簡單的數據,請查看此處的文檔:http://developer.android.com/guide/topics/data/data-storage.html具體而言,共享首選項布爾存儲可以做到這一點。

在簡歷上,您應該從共享首選項中獲取值並使用它們設置複選框。暫停時,您應該從複選框中獲取值並將其放入共享的首選項中。

在查看HeloViews教程時,我也遇到了這個http://developer.android.com/reference/android/preference/CheckBoxPreference.html

只是一些閱讀,可能會得到一些代碼示例,如果我有時間後。 希望這有助於。

+0

繼承人HelloViews教程http://developer.android.com/resources/tutorials/views/hello-formstuff.html#Checkbox – Cooltrooper

+0

我現在說那個bu我已經嘗試了很多次,但是現在我不知道如何獲得一個複選框的共享首選項,如果我有,我現在不會如何將它放入我編輯的代碼中。所以我的問題是,你能爲我寫一個簡單的代碼嗎? – Gaauwe

+0

我正在爲你編寫一段代碼,但我在使用複選框的setChecked方法時不斷出現錯誤,所以我對此也有點不解。儘管如此,我仍會繼續努力。 – Cooltrooper