简体   繁体   English

Android保存并检查,单选按钮

[英]Android Save and check, radio button

Sorry for my bad english. 对不起,我的英语不好。

I have 2 classes, 我有两节课

1 MainActivity.java (Standard) 2 settings.java (for settings) 1 MainActivity.java (标准)2 settings.java (用于设置)

I have a RadioGroup with 5 Radiobuttons . 我有一个带有5个RadiobuttonsRadioGroup

I save the state of the radio buttons as follows (in to settings.java class): 我将单选按钮的状态保存如下(在settings.java类中):

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

    SharedPreferences settings = getSharedPreferences("status", 0); 
    SharedPreferences.Editor editor = settings.edit();

    editor.putBoolean("blue", blue.isChecked());
    editor.putBoolean("orange", orange.isChecked());
    editor.putBoolean("purple", purple.isChecked());
    editor.putBoolean("grey", grey.isChecked());
    editor.putBoolean("green", green.isChecked());
    editor.commit();

}


public void loadSettings () {



    SharedPreferences settings = getSharedPreferences("status", 0);

    royalBlue.setChecked(settings.getBoolean("blue", false)); 
    orange.setChecked(settings.getBoolean("orange", false));
    purple.setChecked(settings.getBoolean("purple", false));
    titan.setChecked(settings.getBoolean("grey", false));
    eighties.setChecked(settings.getBoolean("green", false));

}

the status of the radio buttons is saved successfully. 单选按钮的状态保存成功。 Even after a restart of the app the radio button is saved. 即使重新启动应用程序,单选按钮也会被保存。

I would like now when the RadioButton orange (or other) is selected, the ImageButton will change my image. 我现在想选择RadioButton橙色(或其他)时, ImageButton将更改我的图像。 I would like to make in the MainActivity . 我想在MainActivity

I have tried it in the Main Activity so but I always get a NullPoinException : 我已经在Main Activity中尝试过它,但是我总是得到NullPoinException

Code from the Main ... 主代码...

private settings load = new settings();
...
...
public void change (){
   if (load.orange.isChecked()){
        imBuOn.setImageResource(R.drawable.orange);
    }

as I said I can so unfortunately unable to access the status of the radio button. 如我所说,很不幸,我无法访问单选按钮的状态。

Do I need to maybe use PreferenceManager ? 我是否需要使用PreferenceManager how shall I put it best? 我该如何做到最好?

You don't need to create an instance of the settings class and fetch the value of the checkBox . 您无需创建settings类的实例并获取checkBox的值。 Instead, you should just use the same code as given in the loadSettings method since you are just accessing the SharedPreferences file. 相反,您应该只使用与loadSettings方法中给出的相同的代码,因为您只是在访问SharedPreferences文件。

So, in your MainActivity, just run this 因此,在您的MainActivity中,只需运行

private void checkAndSetImage()
{
     SharedPreferences settings = getSharedPreferences("status", 0);
     if(settings.getBoolean("orange", false))
     {
         imBuOn.setImageResource(R.drawable.orange);
     }
}

Just call this function to wherever applicable. 只要在适当的地方调用此函数即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM