Android: How to check / uncheck RadioButton checkbox inside RadioGroup programmatically in Java

I have actions, MainActivity and settingsActivity. Because of this, I use the methods onPauseand onResumein settingsActivity. So the things that I did in the settings are saved after switching to MainActivity and back.

settingsActivity:

Here I have TextView(called "settings2") as a kind of variable and my three RadioButtons( radio0, radio1and radio2) that are inside RadioGroup. After switching to MainActivity, my program puts "0" in the file (which is saved on the SD card) if the last checkbox is selected radio0. But if the last checkbox radio1, it puts 1 in this file. And if the last checkbox radio2was checked, it puts 2 in this file. I used this in a method onPause.

Then in the method onResumeI read the text of this file and put it in TextView"settings2". After this code (still in onResume) I want to check / uncheck my own RadioButtons. So if the text TextView"settings2" is "0", check RadioButton "radio0"and the rest will not. If the text of this TextViewis "1", you should check RadioButton "radio1", but the rest will not. And if the text of this TextViewis "2", the mark RadioButton "radio2"should be checked, and the rest not. For this, I used the following two codes, but unfortunately they did not work.

First code:

if (settings2.getText().equals("0")) {

        radio0.setChecked(true);
        radio1.setChecked(false);
        radio2.setChecked(false);

    } else if (settings2.getText().equals("1")) {

        radio0.setChecked(false);
        radio1.setChecked(true);
        radio2.setChecked(false);

    } else if (settings2.getText().equals("2")) {

        radio0.setChecked(false);
        radio1.setChecked(false);
        radio2.setChecked(true);

    } 

Second code:

if (settings2.getText().equals("0")) {

        radioGroup1.check(R.id.radio0);

    } else if (settings2.getText().equals("1")) {

        radioGroup1.check(R.id.radio1);

    } else if (settings2.getText().equals("2")) {

        radioGroup1.check(R.id.radio2);

    } 

Can someone help with this little problem please? I look forward to your help!

Thanks in advance!

+3
source share
2

.

 EditText et = ....
 et.getText() // Does not return a string, it returns an Editable.

:

String str = et.getText().toString;

, str, .

: . , . , Strings overriden . String, u .

public boolean More ...equals(Object anObject) {
    if (this == anObject) {
        return true;
    }
    if (anObject instanceof String) {
        String anotherString = (String)anObject;
        int n = count;
        if (n == anotherString.count) {
            char v1[] = value;
            char v2[] = anotherString.value;
            int i = offset;
            int j = anotherString.offset;
            while (n-- != 0) {
                if (v1[i++] != v2[j++])
                    return false;
            }
            return true;
        }
    }
    return false;
}
+4

, TextView 0, 1 2? SharedPreferences! .

, , , settings2.getText().toString (),

int input = Integer.parseInt (settings2.getText().toString()

switch(input) {
    case 0:
        // code when text equals 0
    break;
    case 1:
        // code when text equals 1
    break;
    case 2:
        // code when text equals 2
    break;
}

., .

EDIT: .

2: SharedPreferences

//get your app Preference Manager
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); // If you are coding this in your Activity class, you have to use getDefaultSharedPreferences(this) instead!

public int getPrefs(String key) {
    //get an Integer from the preferences
    return prefs.getInt(key, defaultValue);
    //defaultValue is in case a value for the given key is not found, for example, the user runs the app for the 1st time.
}

public void setPrefs() {
    //You need a SharedPreference editor
    SharedPreferences.Editor prefsEditor = prefs.edit();
    //SharedPreference work with a key and its value
    prefsEditor.putInt(key, value);
    //You have to commit the preferences, or they don't get saved!
    //If you want to use a save button, you can make the Editor variable into a Global var (class variable) and in your save button onClick, just commit!
    prefsEditor.commit();
}
+3

All Articles