【问题标题】:How to save the state of the checkbox when it is checked and a button pressed选中并按下按钮时如何保存复选框的状态
【发布时间】:2013-06-24 23:12:50
【问题描述】:

目的是在选中复选框并按下按钮时保存文本视图的背景颜色,而在重做时它将恢复到正常状态。

我知道要做到这一点,我可以使用共享首选项,但不知何故它不起作用(未保存)。这是我使用的代码(复选框是通过编程方式创建的,而不是通过 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();
                }
            });
        }
    }

}

【问题讨论】:

  • 您好,我也发现了这一点,但我希望在选中复选框并单击按钮时更改文本视图的颜色...不更改复选框的颜色...我希望如果有人可以提供一些代码来寻求帮助,那是为了我的学校项目......谢谢
  • 我在这里看不到任何明显的错误,你的代码重新加载颜色呢?
  • 我在这里需要一个吗?我没有这样做对不起我不知道,因为我只是一个 android 的初学者,你能提供一些代码来寻求帮助......非常感谢 :)
  • 任何认真的人都可以帮助我解决这个问题我被困了一段时间..搜索解决方案但情况变得更糟,该行甚至根本没有改变颜色

标签: java android


【解决方案1】:

我不确定你想说什么,但你可以试试这个。

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

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

boolean check = checkBox.isSelected();

方法返回一个布尔值。

【讨论】:

  • 没有得到link的答复
【解决方案2】:

我会怎么做:

  • 你已经设置了你的复选框(我会称之为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 中添加每个视图也是一个好习惯,因为这会产生很大的错误!

【讨论】:

    【解决方案3】:
    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);
    
            }
        });
    }
    
    }
    

    我已经测试过了;它有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-22
      • 2014-10-24
      相关资源
      最近更新 更多