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

3
我有两个活动,MainActivity和settingsActivity。因此我在settingsActivity中使用了onPause和onResume方法。这样,在切换到MainActivity并返回后,我所做的设置就会被保存。
settingsActivity:
这里我有一个TextView(名为“settings2”)作为一种变量,以及我的三个RadioButtons(radio0、radio1和radio2),它们位于RadioGroup内。 在切换到MainActivity后,如果上次选中的是radio0,则将“0”放入文件中(该文件保存在sdcard上)。但是,如果上次选中的是radio1,则将1放入该文件中。如果上次选中的是radio2,则将2放入该文件中。 我在onPause方法中使用了这个。
然后在onResume方法中,我读取了该文件的文本,并将其放入TextView“settings2”中。 在这段代码之后(仍在onResume中),我想要检查/取消选中我的RadioButtons。因此,如果TextView“settings2”的文本为“0”,则应选中RadioButton“radio0”,而其他RadioButton不选中。如果TextView的文本为“1”,则应选中RadioButton“radio1”,而其他RadioButton不选中。如果TextView的文本为“2”,则应选中RadioButton“radio2”,而其他RadioButton不选中。 为此,我使用了以下两个代码,但不幸的是它们没有起作用。
第一个代码:
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);

    } 

第二段代码:

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

    } 

请问有人能帮忙解决这个小问题吗?非常期待您的帮助!

提前感谢您的帮助!

2个回答

4

以下是问题。

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

尝试:

String str = et.getText().toString;

然后使用字符串进行比较。编辑:请参考下面的源代码,了解为什么必须使用字符串进行比较。默认情况下,按引用进行比较,但字符串已经重写了equals方法,基于字符串值进行比较。如果您不使用字符串,将无法获得匹配结果。
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;
}

我实际上并没有使用EditText,而是使用TextView。使用TextView会有同样的问题吗?无论如何,我尝试了一下,但它没有起作用。现在,SettingsActivity崩溃了。Eclipse也告诉我要像这样使用它:String str = settings2.getText().toString(); 我还将其与“==”进行了比较。也许那是错误的,我应该使用“equals()”进行比较? - Piezord
1
无论如何,都将其放入字符串中。只有String类重写了equals方法来比较字符串值,getText方法返回的其他类型都是比较引用。其次,使用equals进行比较。您总是使用equals方法来比较字符串是否匹配。如果使用“==”运算符,则是在询问它们是否占据内存中的相同位置。 - NameSpace
谢谢你提供这么好的解释,让我们知道何时使用"=="和"equals()"! - Piezord

3

你确定TextView文本只可能是0、1或2吗?对于这样的设置,你应该考虑使用SharedPreferences!它更容易使用,速度也更快。

第二件事是,你应该改为获取 settings2.getText().toString() 的值并将其转换为整数:

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

然后再使用input进行操作。

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

请看这里。我目前在使用手机。

编辑:为了更好的查看,格式化了文本。

编辑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();
}

这个解决方案也可以。只要他将其放入基于值而不是(仅)引用评估相等性的东西中,它就有机会工作。字符串和整数都可以做到这一点。 - NameSpace
我之前尝试过使用sharedPreferences,但是我没有成功。因此,我在Android上的Java方面还比较新,我想问一下您是否能给我展示一个带有RadioButtons的示例脚本?这将帮助我更好地理解您的解决方案的使用!当然,我首先尝试了使用它,但我得到了以下错误:The method parseInt(String) in the type Integer is not applicable for the arguments (CharSequence)。在这种情况下,这是什么意思? - Piezord
1
错误是我的错,settings2.getText() 返回一个 CharSequence 变量,但我们需要的是一个 String,所以我们必须将 CharSequence 转换为 String,这可以通过添加 .toString() 来实现(例如:settings2.getText().toString())。 - Aftab Safdar
1
如果您不急,我想建议您观看两个播放列表!这就是我学习的方式。搜索“ TheNewBoston的Android教程”和“ slideNerd的Android教程”。(我不确定是否允许发布外部链接,因此我不会这样做,但您可以在Google / YouTube上搜索它们) - Aftab Safdar
在你学到更多之前,你可以继续使用像其他答案中的 str.equals(...) 这样的方式。如果你使用我的方法,你必须确保 settings 包含一个数字! - Aftab Safdar
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接