我仍然有这个单选按钮的问题:(。我仍在尝试创建一个设置页面,允许用户选择他们的文本颜色,一旦他们从这个设置活动和所有其他活动中选择了听共享首选项的变化以及.
我知道如何保存文本并从活动之间的共享偏好中获取文本但是 int ?因为我需要单选检查值,所以此方法将传递给其他活动以设置这些文本视图颜色。
谁能帮我检查一下代码吗?
我对 android 和一般编程非常陌生。
public class Text_Colour extends Activity implements OnSharedPreferenceChangeListener {
RadioButton rb1, rb2, rb3, rb4, rb5;
TextView tv1, tv2;
RadioGroup rg1;
Button bt1;
String red, yellow, green, blue;
public static final String PREFS_NAME = "MyPrefsFile";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.colours);
loadPreferences();
tv1 = (TextView)findViewById(R.id.textview1);
tv2 = (TextView)findViewById(R.id.textview2);
bt1 = (Button) findViewById(R.id.button1);
rb1 = (RadioButton) findViewById(R.id.radioButton1);
rb2 = (RadioButton) findViewById(R.id.radioButton2);
rb3 = (RadioButton) findViewById(R.id.radioButton3);
rb4 = (RadioButton) findViewById(R.id.radioButton4);
rb5 = (RadioButton) findViewById(R.id.radioButton5);
rg1 = (RadioGroup) findViewById(R.id.radiogroup1);
bt1.setOnClickListener(Colour_change_b);
//tv1.setOnSharedPreferenceChangeListener(this);
}
View.OnClickListener Colour_change_b = new View.OnClickListener() {
public void onClick(View v) {
if(v == bt1) {
if (rb1.isChecked()== true) {
tv2.setTextColor(Color.RED);
}
if (rb2.isChecked() == true) {
tv2.setTextColor(Color.YELLOW);
}
if (rb3.isChecked() == true) {
tv2.setTextColor(Color.GREEN);
}
if (rb4.isChecked() == true) {
tv2.setTextColor(Color.BLUE);
}
if (rb5.isChecked() == true) {
rb5.getId();
tv2.setTextColor(Color.BLACK);
}
else {
}
SharedPreferences settings1 = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings1.edit();
editor.putInt("colour", rb1.getId());
editor.commit();
finish();
}
}
};
private void loadPreferences() {
// TODO Auto-generated method stub
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
tv1.setTextColor(settings.getId("colour", ""));
settings.registerOnSharedPreferenceChangeListener(Text_Colour.this);
}
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
loadPreferences();
}
}
非常感谢你们的时间。