我想有这样的功能 - 我有一个Dialog(带设置),其中有一个带有两个RadioButtons的RadioGroup。当我使用“保存”按钮关闭对话框时,我不仅希望将数据保存在我的Data类中,而且还希望在下次打开时将Dialog打开并保存设置。我试图用RadiGroup方法实现这个:getCheckedRadioButtonId()和.check(int),但我的解决方案不起作用,我不知道为什么。
我的代码:
public class SettingsDialogFragment extends DialogFragment{
private boolean ifMale;
private int checkedRadio;
private RadioGroup rg;
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.settings);
View view = LayoutInflater.from(getActivity()).inflate(R.layout.settings_dialog, null);
builder.setView(view);
builder.setPositiveButton(R.string.save, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Data.ifMale = ifMale;
checkedRadio = rg.getCheckedRadioButtonId();
System.out.println("numer radio" +checkedRadio);
}
});
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
rg = (RadioGroup) view.findViewById(R.id.radio_group);
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch(checkedId) {
case R.id.radio_female:
//Log.v("RadioGroup", "female");
ifMale = false;
break;
case R.id.radio_male:
Log.v("RadioGroup","male");
ifMale = true;
break;
}
}
});
rg.check(checkedRadio);
return builder.create();
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RadioGroup android:id="@+id/radio_group"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<RadioButton android:id="@+id/radio_female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/female"/>
<RadioButton android:id="@+id/radio_male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/male"/>
</RadioGroup>
</LinearLayout>
答案 0 :(得分:0)
您可以使用Shared Preference
跟踪已选中的单选按钮的信息,并在下次打开对话框时进行检查。
代码段:
SharedPreferences pref = getApplicationContext().getSharedPreferences("RadioPref", MODE_PRIVATE);
Editor editor = pref.edit();
//save data in shared preference on button click
builder.setPositiveButton(R.string.save, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Data.ifMale = ifMale;
checkedRadio = rg.getCheckedRadioButtonId();
System.out.println("numer radio" +checkedRadio);
editor.putInt("checkedId", checkedRadio);
}
});
//get checkedId from shared preference and check that radion button.
int checkedId = pref.getInt("checkedId", -1);
if(checkedId != -1)
{
//rg.check(checkedId);
RadioButton rbutton =(RadioButton)findViewById(checkedId);
rbutton.setChecked(true);
}
希望这有帮助。
答案 1 :(得分:0)
对话框设置代码
public class SettingsDialogFragment extends DialogFragment{
private boolean ifMale;
private int checkedRadio;
private RadioGroup rg;
private SharedPreferences pref;
public Dialog onCreateDialog(Bundle savedInstanceState) {
pref = getApplicationContext().getSharedPreferences("RadioPref", MODE_PRIVATE);
Editor editor = pref.edit();
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.settings);
View view = LayoutInflater.from(getActivity()).inflate(R.layout.settings_dialog, null);
builder.setView(view);
builder.setPositiveButton(R.string.save, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Data.ifMale = ifMale;
checkedRadio = rg.getCheckedRadioButtonId();
System.out.println("numer radio" +checkedRadio);
}
});
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
rg = (RadioGroup) view.findViewById(R.id.radio_group);
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch(checkedId) {
case R.id.radio_female:
//Log.v("RadioGroup", "female");
ifMale = false;
checkedRadio = rg.getCheckedRadioButtonId();
editor.putInt("checkedId", checkedRadio);
break;
case R.id.radio_male:
Log.v("RadioGroup","male");
ifMale = true;
checkedRadio = rg.getCheckedRadioButtonId();
editor.putInt("checkedId", checkedRadio);
break;
}
}
});
rg.check(checkedRadio);
return builder.create();
}
onStart() {
int id=pref.getInt("checkedId");
rg.check(id);
}
}