目标是在选中复选框并按下按钮时保存文本视图的背景颜色,同时在重做时它将恢复到正常状态。
我知道要这样做我可以使用共享偏好但不知何故它不起作用(不保存)。这是我使用过的代码(复选框是以程序方式创建的,不是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 :(得分:0)
我不确定你想说什么,但你可以尝试一下。
boolean check = :JCheckBox reference:.isSelected();
如果JCheckBox引用是checkBox,那么它看起来就像这样。
boolean check = checkBox.isSelected();
方法返回一个布尔值。
答案 1 :(得分:0)
我将如何做到:
checkBox
)和colorEditor
)首先,您需要在SharedPreferences中创建两个条目:DEFAULT_COLOR
和CURRENT_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”!
这也是一个很好的做法,以编程方式添加每个View或从XML添加,因为它是一个很大的错误!
答案 2 :(得分: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);
}
});
}
}
我测试了这个;它有效。