大家好,在我的应用程序中,我正在使用 AlertDialog,我有 3 个值。用户选择一个值,选择的值存储在 sharepreference 中。我想使用这个保存的值,并且默认情况下希望将此值设置为在单选按钮中选择的值。以下是我的代码。
private void getFontSize()
{
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this);
final SharedPreferences.Editor editor = sharedPreferences.edit();
String selectedSize = sharedPreferences.getString("fontSize", "Medium");
final CharSequence[] items = {" Small "," Medium "," Large "};
ArrayList<CharSequence> arrItems=new ArrayList<CharSequence>(Arrays.asList(items));
int prevSelectedIndex = arrItems.indexOf(selectedSize);
// Creating and Building the Dialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select The Font Size");
builder.setSingleChoiceItems(items, prevSelectedIndex, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
switch(item)
{
case 0:
Constants.fontSize = "Small";
editor.putString("fontSize",Constants.fontSize);
editor.commit();
break;
case 1:
Constants.fontSize = "Normal";
editor.putString("fontSize",Constants.fontSize);
editor.commit();
break;
case 2:
Constants.fontSize = "Large";
editor.putString("fontSize",Constants.fontSize);
editor.commit();
break;
}
levelDialog.dismiss();
}
});
levelDialog = builder.create();
levelDialog.show();
}
在这里,如果保存的值为 null,那么我使用“Medium”作为 selectedSize 值。我如何显示选定的单选按钮,因此当用户再次运行此应用程序时,它会显示选中的中等单选按钮(或其他,如果用户选择其他)。任何建议将不胜感激,在此先感谢
.