简体   繁体   English

Android:如何在Java中以编程方式检查/取消选中RadioGroup中的RadioButton

[英]Android: How to check/uncheck a RadioButton inside a RadioGroup programmatically in Java

I have to activities, MainActivity and settingsActivity. 我要活动,MainActivity和settingsActivity。 Because of that I'm using the methods onPause and onResume in the settingsActivity. 因为我在settingsActivity中使用onPauseonResume方法。 So that the things I have done in the settingsActivity are saved after switching to MainActivity and back again. 因此,在切换到MainActivity并再次返回后,我在settingsActivity中完成的操作将被保存。

settingsActivity: settingsActivity:

Here I have a TextView (called "settings2") as a kind of variable and my three RadioButtons ( radio0 , radio1 and radio2 ) which are inside a RadioGroup . 在这里,我有一个TextView (称为“settings2”)作为一种变量和我的三个RadioButtonsradio0radio1radio2 ),它们在RadioGroup After switching to the MainActivity my programe puts "0" in a file (which is saved on the sdcard) if radio0 was last checked. 切换到MainActivity后,如果上次检查radio0 ,我的程序会在文件中保存“0”(保存在SD卡上)。 But if radio1 was last checked, it puts 1 in that file. 但是如果上次检查了radio1 ,它会在该文件中放置1。 And if radio2 was last checked, it puts 2 in that file. 如果上次检查了radio2 ,它会在该文件中放入2。 I used this in the method onPause . 我在onPause方法中使用了这个。

Then in the method onResume I read the text of that file and put it in the TextView "settings2". 然后在onResume方法中,我读取该文件的文本并将其放在TextView “settings2”中。 After this code (still in onResume ) I want to check/uncheck my RadioButtons . 在此代码之后(仍在onResume )我想检查/取消选中我的RadioButtons So if the text of the TextView "settings2" is "0" the RadioButton "radio0" shall be checked and the others not. 因此,如果TextView “settings2”的文本为“0”,则应检查RadioButton "radio0"而不检查其他。 If the text of this TextView is "1" the RadioButton "radio1" shall be checked and the others not. 如果此TextView的文本为“1”,则应检查RadioButton "radio1"而不检查其他。 And if the text of this TextView is "2" the RadioButton "radio2" shall be checked and the others not. 如果此TextView的文本为“2”,则应检查RadioButton "radio2"而不检查其他。 To do this I used the following two codes but unfortunately they didn't 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'm looking forward to your help! 我期待着你的帮助!

Thanks in advance! 提前致谢!

Here's the problem. 这是问题所在。

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

Try: 尝试:

String str = et.getText().toString;

Then using str to do your comparisons. 然后使用str进行比较。

Edit: See source code below on why u have to use Strings to compare. 编辑:请参阅下面的源代码,了解为什么必须使用字符串进行比较。 The default is to compare by reference, but Strings have overriden equals to compare based on string values. 默认值是通过引用进行比较,但字符串具有覆盖等于基于字符串值进行比较。 If you aren't using String u won't get matches. 如果您不使用String,则不会获得匹配。

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;
}

Are you sure the TextView text is either 0, 1, or 2? 您确定TextView文本是0,1还是2? And for a setting like this, you should look into SharedPreferences ! 对于这样的设置,您应该查看SharedPreferences It is much easier to use and much faster too. 它更容易使用,也更快。

2nd thing you should do is instead of getting the settings2.getText().toString () you should do 你应该做的第二件事是你不应该得到settings2.getText().toString ()

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

and then use 然后使用

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;
}

Look into this. 看看这个。 . I'm on mobile at the moment 我现在在移动设备上

EDIT: Formatted text for better view. 编辑:格式化文本以获得更好的视图。

EDIT 2 : SharedPreferences example 编辑2:SharedPreferences示例

//get your app's 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's onClick, just commit!
    prefsEditor.commit();
}

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

相关问题 如何以编程方式将文本设置为RadioGroup内的RadioButton? - How to set text to RadioButton which is inside of a RadioGroup programmatically? 如何在不带ID的RadioGroup中默认检查RadioButton? - How to default check RadioButton in RadioGroup without Id? 如何将LineaLayout中的RadioButton放在RadioGroup中 - How to put RadioButton that is in LineaLayout inside a RadioGroup 当未选中所有RadioButton时如何知道RadioGroup的RadioButton的第一次检查 - how to know the first check of a RadioButton of a RadioGroup when all of RadioButton in it are unchecked 如何通过Java将RadioButton添加到RadioGroup? - How can i add RadioButton to the RadioGroup by java? 在ListView的RadioGroup内添加RadioButton - Add RadioButton inside RadioGroup in ListView Android Java-是否可以从RadioGroup中的RadioButton获取文本,而无需每个RadioButton具有定义的ID? - Android java - is it possible to get text from RadioButton in RadioGroup without each RadioButton having a defined id? 以编程方式将RadioButton的边距或填充添加到RadioGroup中 - Add margin or padding to RadioButton into a RadioGroup programmatically RadioGroup中的RadioButtons在ListView中随机检查/取消选中 - RadioButtons within a RadioGroup check/uncheck randomly in a ListView 如何在ListView Android中检查RadioGroup中的单选按钮 - How to check the radio button in RadioGroup in ListView Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM