2012-03-24 56 views
0

我一直试图在其他activites中使用/保存复选框的布尔值,但没有多少运气。在其他Activites中使用CheckBox值Android

我知道你必须使用SharedPreferences,但是我无法正确设置它。我已经做了首类具有

private static final String OPTION_PREF = "my.main.project"; 
    private SharedPreferences optionPreferences; 
    private Editor optionEditor; 
    private boolean checkbox; 

    public Preferences(Context context) 
    { 
     this.optionPreferences = context.getSharedPreferences(OPTION_PREF, Activity.MODE_PRIVATE); 
     this.optionEditor = optionPreferences.edit(); 
    } 

    public boolean getChecked() 
    { 
     return optionPreferences.getBoolean("is_checked", checkbox); 
    } 
    public void saveChecked(boolean checkBox) 
    { 
     optionEditor.putBoolean("save_check_pref", checkBox); 
     optionEditor.commit(); 
    } 

然后在例如选项菜单,

boolean veggieChecked; 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.options); 

    Preferences pref; 
    pref = new Preferences(getApplicationContext()); 

    veggieChecked = pref.getChecked(); 

    final CheckBox checkBox = (CheckBox) findViewById(R.id.vegetarian); 
    if(checkBox.isChecked()) 
     veggieChecked = true; 

    pref.saveChecked(veggieChecked); 

我真的不能看到我在做什么错了,因为我是新来的Android和没有使用sharedpreferences之前..任何帮助将不胜感激!

+0

如果你想使用'SharedPreferences',你需要调用'Context.getSharedPreferencs()'再在选项菜单,然后调用'得到() ',而不是做'checkbox.isChecked()'。 – gobernador 2012-03-24 15:04:05

回答

1

您可以使用这样的事情:

CheckBox Equities = (CheckBox) findViewById(R.id.Equities); 
    CheckBox FixedIncome = (CheckBox) findViewById(R.id.FixedIncome); 
    CheckBox Currencies = (CheckBox) findViewById(R.id.Currencies); 
    CheckBox Commodities = (CheckBox) findViewById(R.id.Commodities); 
    CheckBox Derivatives = (CheckBox) findViewById(R.id.Derivatives); 



    myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE); 
    prefsEditor = myPrefs.edit(); 


    int prefEq = myPrefs.getInt("Equities", 0); 
    int prefFI = myPrefs.getInt("FixedIncome", 0); 
    int prefCu = myPrefs.getInt("Currencies", 0); 
    int prefCo = myPrefs.getInt("Commodities", 0); 
    int prefDe = myPrefs.getInt("Derivatives", 0); 

    if(prefEq == 1) 
    { 
     Equities.setChecked(true); 
    } 
    else 
    { 
     Equities.setChecked(false); 
    } 


    if(prefFI == 1) 
    { 
     FixedIncome.setChecked(true); 
    } 
    else 
    { 
     FixedIncome.setChecked(false); 
    } 
    if(prefCu == 1) 
    { 
     Currencies.setChecked(true); 
    } 
    else 
    { 
     Currencies.setChecked(false); 
    } 
    if(prefCo == 1) 
    { 
     Commodities.setChecked(true); 
    } 
    else 
    { 
     Commodities.setChecked(false); 
    } 
    if(prefDe == 1) 
    { 
     Derivatives.setChecked(true); 
    } 
    else 
    { 
     Derivatives.setChecked(false); 
    } 






    Equities.setOnCheckedChangeListener(new OnCheckedChangeListener() 
    { 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
     { 
      if (isChecked) 
      { 
      prefsEditor.putInt("Equities", 1); 
      prefsEditor.commit(); 
      } 
      else 
      { 
      myArray[0] = false; 
      prefsEditor.putInt("Equities", 0); 
      prefsEditor.commit(); 
      } 

     } 
    }); 


    FixedIncome.setOnCheckedChangeListener(new OnCheckedChangeListener() 
    { 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
     { 
      if (isChecked) 
      { 

      prefsEditor.putInt("FixedIncome", 1); 
      prefsEditor.commit(); 
      } 
      else 
      { 

      prefsEditor.putInt("FixedIncome", 0); 
      prefsEditor.commit(); 
      } 

     } 
    }); 


    Currencies.setOnCheckedChangeListener(new OnCheckedChangeListener() 
    { 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
     { 
      if (isChecked) 
      { 

      prefsEditor.putInt("Currencies", 1); 
      prefsEditor.commit(); 
      } 
      else 
      { 

      prefsEditor.putInt("Currencies", 0); 
      prefsEditor.commit(); 
      } 

     } 
    }); 




    Commodities.setOnCheckedChangeListener(new OnCheckedChangeListener() 
    { 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
     { 
      if (isChecked) 
      { 

      prefsEditor.putInt("Commodities", 1); 
      prefsEditor.commit(); 
      } 
      else 
      { 

      prefsEditor.putInt("Commodities", 0); 
      prefsEditor.commit(); 
      } 

     } 
    }); 


    Derivatives.setOnCheckedChangeListener(new OnCheckedChangeListener() 
    { 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
     { 
      if (isChecked) 
      { 

      prefsEditor.putInt("Derivatives", 1); 
      prefsEditor.commit(); 
      } 
      else 
      { 

      prefsEditor.putInt("Derivatives", 0); 
      prefsEditor.commit(); 
      } 

     } 
    }); 
+0

我解决了你的帖子上的一个小变化,帖子号。 3由罗伯特,http://mgmblog.com/2008/02/18/android-checkbox-oncheckedchangelistener/,我有这些语句第一, SharedPreferences myPrefs; myPrefs = this.getSharedPreferences(“myprefs”,MODE_WORLD_READABLE); final Editor prefsEditor = myPrefs.edit(); final CheckBox checkBox =(CheckBox)findViewById(R.id.vegetarian); checkBox.setChecked(myPrefs.getBoolean(“veggieBox”,false)); – TomSelleck 2012-03-24 15:52:49

1

乍一看,你把关键is_checked和后来试图拉save_check_pref

相关问题