【发布时间】: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 的初学者,你能提供一些代码来寻求帮助......非常感谢 :)
-
任何认真的人都可以帮助我解决这个问题我被困了一段时间..搜索解决方案但情况变得更糟,该行甚至根本没有改变颜色