2011-10-15 28 views
2

目的是保存textview的背景颜色,只要复选框被选中并且按下按钮时它将在重做时恢复到正常状态它。如何保存复选框的状态,当它被选中并且按下一个按钮时

我知道这样做我可以使用共享首选项,但不知何故它不起作用(不保存)。这里是代码,我已经使用(复选框创建程式设计虽然不是XML)

status=(Button)findViewById(R.id.status); 
CheckBox checkbox = new CheckBox(myContext); 
tr.addView(checkbox); 

checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener(){ 

    public void onCheckedChanged(CompoundButton arg0, boolean isChecked) { 
     // TODO Auto-generated method stub 
     if (isChecked){ 
      status.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View arg0) { 
        // TODO Auto-generated method stub 
        //myEditor.putInt("backColor", Color.LTGRAY); 
        //tr.setBackgroundColor(Color.LTGRAY); 
        mySharedPreferences=getSharedPreferences(MYPREFS,0); 
        SharedPreferences.Editor myEditor; 
        myEditor=mySharedPreferences.edit(); 
        final int backColor=mySharedPreferences.getInt("color", Color.LTGRAY); 
        tr.setBackgroundColor(backColor); 
        myEditor.putInt("color", backColor); 
        myEditor.commit(); 
       } 
      }); 
     } 
    } 

} 
+0

http://stackoverflow.com/questions/6800244/android-how-to-save-the-state-of-a-checkbox –

+0

嗨,我也发现了这一点,但我希望textview的颜色改变颜色当一个复选框被选中并且一个按钮被点击时......不改变复选框的颜色......我希望任何人都可以提供一些帮助代码,这是为了我的学校项目......谢谢 – user996824

+0

我看不到任何东西显然这里错了,你的代码重新加载颜色怎么样? – dten

回答

0

林不知道你想说什么,但你可以试试这个。

boolean check = :JCheckBox reference:.isSelected(); 

如果JCheckBox引用是checkBox,那么它看起来像这样。

boolean check = checkBox.isSelected(); 

方法返回一个布尔值。

+1

没有得到一个答案[链接](http://docs.oracle.com/javase/1.4.2 /docs/api/javax/swing/JCheckBox.html) – sabbibJAVA

0

我会怎么做:

  • 你有你的复选框,设置了(我称它为checkBox)和
  • 你有你的颜色编辑器(这将是colorEditor

因此首先您需要在SharedPreferences中创建两个条目:DEFAULT_COLORCURRENT_COLOR

SharedPreferences.Editor editor = preferences.edit(); 
editor.putInt("DEFAULT_COLOR", 0xFFFFFFFF); // we will not touch this later 
editor.putInt("CURRENT_COLOR", 0xFFFFFFFF); //set to DEFAULT, will change this. 
editor.commit(); 

现在目前在onCheckedChanged你只需要做到这一点:

public void onCheckedChanged(CompoundButton checkBox, boolean isChecked) 
{ 
    SharedPreferences preferences = ...; //just get the SharedPref. object 
    SharedPreferences.Editor editor = preferences.edit(); 
    int color = preferences.getInt("DEFAULT_COLOR", -1); //get the default color 

    // change it if the CheckBox is checked 
    if(isChecked) 
     color = colorEditor.getColor(); //get the color from wherever you want 

    editor.putInt("CURRENT_COLOR", color); 
    editor.commit(); 

    //set the color as background. 
} 

也不要忘记在每次启动应用程序时的背景颜色设置为从SharedPreferences的“CURRENT_COLOR”!

此外,以编程方式或从XML中添加每个视图是一种很好的做法,因为这是一大错误!

0
package com.android.app; 

import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.CheckBox; 
import android.app.Activity; 
import android.content.SharedPreferences; 

public class MainActivity extends Activity { 
CheckBox cb; 
Button save,load; 
SharedPreferences sp; 
public static String filename=("Folder"); 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    cb=(CheckBox)findViewById(R.id.checkBox1); 
    save=(Button)findViewById(R.id.bsave); 
    load=(Button)findViewById(R.id.bload); 
    sp=getSharedPreferences(filename,0); 
    save.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      boolean b=cb.isChecked(); 
      SharedPreferences.Editor editor=sp.edit(); 
      editor.putBoolean("str",b); 
      editor.commit(); 
      finish(); 

     } 
    }); 
    load.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      sp=getSharedPreferences(filename,0); 
      boolean bool=sp.getBoolean("str",false); 
      cb.setChecked(bool); 

     } 
    }); 
} 

} 

我测试过这个;有用。

相关问题