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;
I have an application with checkboxes, How can i save them?
My layout is:
<?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>
(Yes i now, that are much checkboxes)
And my main.class is:
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;
}
}
Do you now how to do this?
I hope you understand me, and i hope you could help me.
Gaauwe
EDIT:
Here is the code that i want to save things and load things:
@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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
SharedPreferences 怎么样?您可以以键:值格式存储和加载复选框的状态和名称。 如何使用。
例如像这样的东西:
但是,您应该实现更有效的方法,可能是循环遍历所有复选框以读取其名称或ID,您可以在离开活动之前调用它。
How about SharedPreferences? You can store and load the state and the name of the checkbox in a key:value format. How to use.
E. g. something like this:
However you should implement more efficient way, probably a loop through all the checkboxes to read its name or id, which you can call before leaving the activity.
重写 onSaveInstanceState(Bundle outState) 并写入哪些已签入 outState。然后,您可以通过从传递给 onCreate(Bundle savingInstanceState) 的 savingInstanceState 中提取信息来查看创建活动时检查了哪些内容。
例如,
重要提示:不保证将调用“onSaveInstanceState”。因此,仅当保存 ui 状态是为了方便用户而不是对功能至关重要时才使用此选项。如果您不在用户的设备上保存数据(即使用 SharedPreference),用户会更高兴。
Override onSaveInstanceState(Bundle outState) and write which ones are checked into outState. Then you can see which ones are checked when the activity is create by extracting that information from savedInstanceState passed to onCreate(Bundle savedInstanceState)
For example,
IMPORTANT: There is no guarantee that 'onSaveInstanceState' will be called. So only use this if saving the ui state is for convenience for the user and not vital to functionality. User will be happier if you don't save data on their device, ie by using SharedPreference.
如果您想跨会话保存简单数据,请查看此处的文档: http://developer.android.com/guide/topics/data/data-storage.html 特别是共享首选项布尔存储可以解决这个问题。
在恢复时,您应该从共享首选项中获取值并使用它们设置复选框。暂停时,您应该从复选框中获取值并将它们放入共享首选项中。
我也遇到了这个 http://developer.android.com/reference/android/在查看 HeloViews 教程时,preference/CheckBoxPreference.html。
只是阅读一些内容,如果以后有时间的话,可能会抽出时间给您一些代码示例。
希望这有帮助。
If you want to save simple data across sessions take a look at the documentation here: http://developer.android.com/guide/topics/data/data-storage.html specifically the shared preferences boolean storage would do the trick.
On resume you should get the values from the shared prefs and set the checkboxes using them. On pause you should get the values from the check boxes and put them into the shared prefs.
I also ran into this http://developer.android.com/reference/android/preference/CheckBoxPreference.html when looking at the HeloViews tutorial.
Just some reading around, might get round to giving you some code examples if I have time later.
Hope this helps.