【发布时间】:2015-01-04 06:58:54
【问题描述】:
我在一个单选组中有三个单选按钮。我基本上通过从 xml 文件中删除 set selected line 来取消选择所有单选按钮。现在,当用户选择任何按钮时,它只能工作一次,当它再次进入单选按钮选择窗口时,它在选择状态之前已经丢失了所有内容。我尝试了这么多语句,但没有成功。意味着我想要当用户检查任何按钮时,它将始终保持选中状态,直到用户不检查任何其他按钮。请帮我。下面是我的代码。
SelectedLanguage.java:
public class SelectLanguage extends ActionBarActivity {
public static String lang;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.select_language);
RadioGroup group = (RadioGroup) findViewById(R.id.radioGroup1);
RadioButton gujarati = (RadioButton) findViewById(R.id.radio0);
RadioButton hindi = (RadioButton) findViewById(R.id.radio1);
RadioButton english = (RadioButton) findViewById(R.id.radio2);
//int radio_selected = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE).getInt("my_selected_radio",0);
//int radio_selected = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE).edit().putInt("my_selected_radio",selectedValue).commit();
group.setOnCheckedChangeListener(new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if(checkedId == R.id.radio0) {
lang = "Gujarati";
} else if(checkedId == R.id.radio1) {
lang = "Hindi";
} else {
lang = "English";
}
}
});
}
select_language.xml:
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_marginTop="25dp" >
<RadioButton
android:id="@+id/radio0"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="RadioButton" />
<RadioButton
android:id="@+id/radio1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="RadioButton" />
<RadioButton
android:id="@+id/radio2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="RadioButton" />
</RadioGroup>
【问题讨论】:
-
当另一个活动(屏幕)启动时,前一个活动进入暂停状态,否则如果您完成();它,它被破坏了,所以简单的解决方案是将您选中的项目索引或字符串本地保存在 SharedPreference 中并在 OnCreate() 中检索它们并根据先前的用户选择设置复选框选择,这是您可以做到的最简单的方法
标签: android radio-button status radio-group