Android:如何在 Java 中以编程方式选中/取消选中 RadioGroup 中的 RadioButton
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21662405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: How to check/uncheck a RadioButton inside a RadioGroup programmatically in Java
提问by Piezord
I have to activities, MainActivity and settingsActivity. Because of that I'm using the methods onPause
and onResume
in the settingsActivity. So that the things I have done in the settingsActivity are saved after switching to MainActivity and back again.
我必须活动,MainActivity 和 settingsActivity。因此,我正在使用这些方法onPause
和onResume
settingsActivity。以便我在 settingsActivity 中所做的事情在切换到 MainActivity 并再次返回后保存。
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
.
After switching to the MainActivity my programe puts "0" in a file (which is saved on the sdcard) if radio0
was last checked. But if radio1
was last checked, it puts 1 in that file. And if radio2
was last checked, it puts 2 in that file.
I used this in the method onPause
.
在这里,我有一个TextView
(称为“settings2”)作为一种变量,而我的三个RadioButtons
(radio0
,radio1
和radio2
)位于RadioGroup
. 切换到 MainActivity 后,如果radio0
最后一次检查,我的程序会将“0”放入文件(保存在 SD 卡上)中。但是如果radio1
最后一次检查,它会将 1 放在该文件中。如果radio2
最后一次检查,它会将 2 放入该文件中。我在方法中使用了这个onPause
。
Then in the method onResume
I read the text of that file and put it in the TextView
"settings2".
After this code (still in onResume
) I want to check/uncheck my RadioButtons
. So if the text of the TextView
"settings2" is "0" the RadioButton
"radio0"
shall be checked and the others not. If the text of this TextView
is "1" the RadioButton
"radio1"
shall be checked and the others not. And if the text of this TextView
is "2" the RadioButton
"radio2"
shall be checked and the others not.
To do this I used the following two codes but unfortunately they didn't work.
然后在该方法中,onResume
我读取该文件的文本并将其放入TextView
“settings2”中。在此代码(仍在onResume
)之后,我想选中/取消选中我的RadioButtons
. 因此,如果TextView
“settings2”的文本为“0”,RadioButton
"radio0"
则应检查其他内容。如果此文本TextView
为“1”,RadioButton
"radio1"
则应检查其他内容。如果此文本TextView
为“2”,RadioButton
"radio2"
则应检查其他内容。为此,我使用了以下两个代码,但不幸的是它们不起作用。
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!
提前致谢!
回答by NameSpace
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.
编辑:有关为什么必须使用字符串进行比较,请参阅下面的源代码。默认是通过引用进行比较,但字符串已覆盖 equals 以基于字符串值进行比较。如果您不使用 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;
}
回答by Aftab Hussain
Are you sure the TextView text is either 0, 1, or 2? And for a setting like this, you should look into SharedPreferences
! It is much easier to use and much faster too.
您确定 TextView 文本是 0、1 或 2?对于这样的设置,您应该研究一下SharedPreferences
!它更容易使用,也更快。
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()
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();
}