简体   繁体   中英

Android Checkbox setChecked() has no effect

I'm making a settings page, with a bunch of checkboxes. The user picks the options he wants, and then hits the submit button, which saves the results on a text file, like 1;0;1;1;1;0 etc, with the 1 representing checked and the 0 unchecked.

Upon the next program start, the program will look for the settings file. If it can't find it, the checkboxes are left at their default true vale (all on). If it can, it will read the file, and set the checkboxes accordingly. This last step is where I'm having the problem- using toasts, I can see that the program finds the file, splits it correctly, and has the correct value for the given checkbox. I even have a toast that fires in the if() block where I check if the value is a 0, the line after I use the setChecked() code. That toast fires, so the setChecked() code is being read. However, the checkbox does not get updated, and remains checked. My guess would be that the view isn't being refreshed after I change the boxes.

This is the first time I've made an android option that isn't entirely a service, and has a GUI, so I'm a bit unclear on it. What would be the simplest way to quickly get the screen to refresh after the boxes have been set, or is there some other problem?

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //establish checkBoxes
    checkBatteryLevel = (CheckBox) findViewById(R.id.checkBox1);
    checkBatteryVoltage = (CheckBox) findViewById(R.id.checkBox2);
    checkBluetooth = (CheckBox) findViewById(R.id.checkBox5);
    checkCall = (CheckBox) findViewById(R.id.checkBox4);
    checkCharger = (CheckBox) findViewById(R.id.checkBox3);
    checkScreen = (CheckBox) findViewById(R.id.checkBox8);
    checkWifiPermis = (CheckBox) findViewById(R.id.checkBox6);
    checkWifiCnct = (CheckBox) findViewById(R.id.checkBox7);
    submitButton = (Button) findViewById(R.id.button1);

    Toast toast = Toast.makeText(getApplicationContext(), "Text here", Toast.LENGTH_LONG);

    //check if text settings are there
    textSettings = new File (root, "UsageMonitorSettings.txt");
    if(textSettings.exists())
    {
        toast = Toast.makeText(getApplicationContext(), "File Exists", Toast.LENGTH_LONG);
        toast.show();
        //if present, read it and set buttons accordingly
        try {
            Scanner myScanner = new Scanner(textSettings);
            while (myScanner.hasNextLine())
            {
                String line = myScanner.next();
                toast = Toast.makeText(getApplicationContext(), line, Toast.LENGTH_LONG);
                toast.show();
                String[] lineArray = line.split(";");
                toast = Toast.makeText(getApplicationContext(), lineArray[0], Toast.LENGTH_LONG);
                toast.show();
                if(lineArray[0].equals("0"))
                {
                    checkBatteryLevel.setChecked(false);
                    toast = Toast.makeText(getApplicationContext(), "batt check changed", Toast.LENGTH_LONG);
                    toast.show();
                }
                if(lineArray[1].equals("0"))
                {
                    checkBatteryVoltage.setChecked(false);
                }
                if(lineArray[2].equals("0"))
                {
                    checkCharger.setChecked(false);
                }
                if(lineArray[3].equals("0"))
                {
                    checkCall.setChecked(false);
                }
                if(lineArray[4].equals("0"))
                {
                    checkBluetooth.setChecked(false);
                }
                if(lineArray[5].equals("0"))
                {
                    checkWifiPermis.setChecked(false);
                }
                if(lineArray[6].equals("0"))
                {
                    checkWifiCnct.setChecked(false);
                }
                if(lineArray[7].equals("0"))
                {
                    checkScreen.setChecked(false);
                }
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
    else
    {
        toast = Toast.makeText(getApplicationContext(), "File does not exist", Toast.LENGTH_LONG);
        toast.show();
        //if not, make one with all defaulting to on
        try {
            BufferedWriter myFileWriter = new BufferedWriter(new FileWriter(textSettings));
            myFileWriter.write("1;1;1;1;1;1;1;1");
            myFileWriter.flush();
            myFileWriter.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    submitButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            //when button hit update text settings with checked values
            try {
                BufferedWriter myFileWriter = new BufferedWriter(new FileWriter(textSettings, true));
                if(checkBatteryLevel.isChecked())
                {
                    myFileWriter.write("1"+";");
                }
                else
                {
                    myFileWriter.write("0"+";");
                }
                if(checkBatteryVoltage.isChecked())
                {
                    myFileWriter.write("1"+";");
                }
                else
                {
                    myFileWriter.write("0"+";");
                }
                if(checkCharger.isChecked())
                {
                    myFileWriter.write("1"+";");
                }
                else
                {
                    myFileWriter.write("0"+";");
                }
                if(checkCall.isChecked())
                {
                    myFileWriter.write("1"+";");
                }
                else
                {
                    myFileWriter.write("0"+";");
                }
                if(checkBluetooth.isChecked())
                {
                    myFileWriter.write("1"+";");
                }
                else
                {
                    myFileWriter.write("0"+";");
                }
                if(checkWifiPermis.isChecked())
                {
                    myFileWriter.write("1"+";");
                }
                else
                {
                    myFileWriter.write("0"+";");
                }
                if(checkWifiCnct.isChecked())
                {
                    myFileWriter.write("1"+";");
                }
                else
                {
                    myFileWriter.write("0"+";");
                }
                if(checkScreen.isChecked())
                {
                    myFileWriter.write("1");
                }
                else
                {
                    myFileWriter.write("0");
                }
                myFileWriter.flush();
                myFileWriter.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });
}

And here are the relevant parts of main.xml:

<CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="138dp"
    android:layout_height="wrap_content"
    android:text="Battery Level" 
    android:checked="true" />


<CheckBox
    android:id="@+id/checkBox2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Battery Voltage" 
    android:checked="true" />

<CheckBox
    android:id="@+id/checkBox5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Bluetooth State" 
    android:checked="true" />

<CheckBox
    android:id="@+id/checkBox4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Call State" 
    android:checked="true" />

<CheckBox
    android:id="@+id/checkBox3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Charger State" 
    android:checked="true" />

<CheckBox
    android:id="@+id/checkBox8"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Screen State" 
    android:checked="true" />

<CheckBox
    android:id="@+id/checkBox6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Wifi Permission" 
    android:checked="true" />

<CheckBox
    android:id="@+id/checkBox7"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Wifi Connection" 
    android:checked="true" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Start Service" />

The problem is probably that,

  • with onCreate, you first read the settings if they exist, then you make the onClickListeners.
  • what do you do in the method onResume? Do you do anything there?!!

This means that you should look a bit more into the Android Activity Lifecycle .

Also, your settings file is not a good implementation of preferences. Unless you want 1 application to read another Android application's settings, you should look into SharedPreferences for storing application-wide settings - it's real easy to use.

If you want a simple UI, and want to finish quickly, there is a special activity designed especially for settings UI: PreferenceActivity - although later on you may find it lacks some functions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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