我有一个带复选框的应用程序,我该如何保存它们?
我的布局是:
<?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 ;@OverrIDepublic 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)); } } });}@OverrIDepublic 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;}@OverrIDepublic 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;
@R_502_6120@:
SharedPreferences怎么样?您可以以键:值格式存储和加载复选框的状态和名称. How to use.
E. g.这样的事情:
// Init SharedPreferences settings = getSharedPreferences("mysettings", 0);SharedPreferences.Editor editor = settings.edit();// Saveboolean checkBoxValue = v42.isChecked(); editor.putBoolean("v42", checkBoxValue); editor.commit();;// Loadv42.setChecked(settings.getBoolean("v42", false));
但是,您应该实现更有效的方法,可能是通过所有复选框循环读取其名称或ID,您可以在离开活动之前调用它.
总结以上是内存溢出为你收集整理的Android – 如何保存CheckBox的状态全部内容,希望文章能够帮你解决Android – 如何保存CheckBox的状态所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)