Java 如何在选中复选框并按下按钮时保存复选框的状态
其目的是在选中复选框并按下按钮时保存文本视图的背景色,而在重做时,文本视图将恢复到正常状态 我知道,要做到这一点,我可以使用共享首选项,但不知何故,它不起作用(未保存)。以下是我使用的代码(复选框是通过编程而不是xml创建的)Java 如何在选中复选框并按下按钮时保存复选框的状态,java,android,Java,Android,其目的是在选中复选框并按下按钮时保存文本视图的背景色,而在重做时,文本视图将恢复到正常状态 我知道,要做到这一点,我可以使用共享首选项,但不知何故,它不起作用(未保存)。以下是我使用的代码(复选框是通过编程而不是xml创建的) 我不知道你想说什么,但你可以试试这个 boolean check = :JCheckBox reference:.isSelected(); 如果JCheckBox引用是checkBox,那么它将如下所示 boolean check = checkBox.isSelec
我不知道你想说什么,但你可以试试这个
boolean check = :JCheckBox reference:.isSelected();
如果JCheckBox引用是checkBox,那么它将如下所示
boolean check = checkBox.isSelected();
方法返回一个布尔值。我不确定您想说什么,但您可以试试这个
boolean check = :JCheckBox reference:.isSelected();
如果JCheckBox引用是checkBox,那么它将如下所示
boolean check = checkBox.isSelected();
方法返回一个布尔值。如何执行该操作:
- 您已设置了复选框(我将其称为
),并且checkbox
- 您有您的颜色编辑器(它将是
)colordeditor
DEFAULT\u COLOR
和CURRENT\u 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.
}
另外,每次启动应用程序时,不要忘记将共享参考资料中的背景颜色设置为“当前颜色”
另外,以编程方式或从XML添加每个视图也是一种很好的做法,因为这是一个很大的错误来源我将如何做:
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);
}
});
}
}
- 您已设置了复选框(我将其称为
),并且checkbox
- 您有您的颜色编辑器(它将是
)colordeditor
DEFAULT\u COLOR
和CURRENT\u 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.
}
另外,每次启动应用程序时,不要忘记将共享参考资料中的背景颜色设置为“当前颜色”
另外,以编程方式或从XML添加每个视图也是一种很好的做法,因为这是一个很大的错误来源强>
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);
}
});
}
}
我已经测试过了;它起作用了
我已经测试过了;它可以工作。您好,我也发现了这一点,但我希望选中复选框并单击按钮时,文本视图的颜色可以更改…而不是更改复选框的颜色。。。我希望如果有人能提供一些代码来帮助我,这是为了我的学校项目…谢谢,我看不出这里有什么明显的错误,你重新加载颜色的代码怎么样?我需要一个吗?我没有这么做,对不起,我不知道,因为我只是一个android初学者,你能提供一些代码来帮助我吗…非常感谢:)任何人都可以帮我这个忙我被困了一段时间..搜索解决方案,但它变得更糟了行甚至没有改变颜色allHi我也发现了这一点,但我希望文本视图的颜色在选中复选框并单击按钮时更改颜色…不更改复选框的颜色。。。我希望如果有人能提供一些代码来帮助我,这是为了我的学校项目…谢谢,我看不出这里有什么明显的错误,你重新加载颜色的代码怎么样?我需要一个吗?我没有这么做,对不起,我不知道,因为我只是一个android初学者,你能提供一些代码来帮助我吗…非常感谢:)任何人都可以帮我这个忙我被困了一段时间..搜索解决方案,但它变得更糟了行甚至没有改变颜色例如:我没有得到答案