如何在 alertDialog 中显示选定的值
how to show selected value in alertDialog
大家好,我在我的应用程序中使用了 AlertDialog,其中我有 3 个值。用户 select 一个值和 selected 值存储在 sharepreference 中。我想使用这个保存的值,默认情况下想在单选按钮中将此值设置为 selected。以下是我的代码。
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();
}
这里如果保存的值为空,那么我使用 "Medium" 作为 selectedSize 值。我如何显示 selected 单选按钮,所以当用户 运行 再次使用此应用程序时,它会将中等单选按钮(或其他,如果用户 selected 其他)显示为 select编辑。
任何建议将不胜感激,提前致谢
.
这就是你想要的:
String selectedSize = sharedPreferences.getString("fontSize", "Medium");
final CharSequence[] items =
{
"Small", "Medium", "Large"
};
int selectedIndex = -1;
for (int i = 0; i < items.length; i++)
{
if(items[i].equals(selectedSize))
{
selectedIndex=i;
break;
}
}
// Creating and Building the Dialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select The Font Size");
builder.setSingleChoiceItems(items, selectedIndex, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int item)
{
switch (item)
{
...
}
}
});
AlertDialog levelDialog = builder.create();
levelDialog.show();
希望对您有所帮助!
将 selectedSize
作为第二个参数传递给 setSingleChoiceItems
方法:
ArrayList<CharSequence> arrItems=new ArrayList<CharSequence>(Arrays.asList(items));
int prevSelectedIndex = arrItems.indexOf(selectedSize);
builder.setSingleChoiceItems(items, prevSelectedIndex,...
大家好,我在我的应用程序中使用了 AlertDialog,其中我有 3 个值。用户 select 一个值和 selected 值存储在 sharepreference 中。我想使用这个保存的值,默认情况下想在单选按钮中将此值设置为 selected。以下是我的代码。
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();
}
这里如果保存的值为空,那么我使用 "Medium" 作为 selectedSize 值。我如何显示 selected 单选按钮,所以当用户 运行 再次使用此应用程序时,它会将中等单选按钮(或其他,如果用户 selected 其他)显示为 select编辑。 任何建议将不胜感激,提前致谢
这就是你想要的:
String selectedSize = sharedPreferences.getString("fontSize", "Medium");
final CharSequence[] items =
{
"Small", "Medium", "Large"
};
int selectedIndex = -1;
for (int i = 0; i < items.length; i++)
{
if(items[i].equals(selectedSize))
{
selectedIndex=i;
break;
}
}
// Creating and Building the Dialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select The Font Size");
builder.setSingleChoiceItems(items, selectedIndex, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int item)
{
switch (item)
{
...
}
}
});
AlertDialog levelDialog = builder.create();
levelDialog.show();
希望对您有所帮助!
将 selectedSize
作为第二个参数传递给 setSingleChoiceItems
方法:
ArrayList<CharSequence> arrItems=new ArrayList<CharSequence>(Arrays.asList(items));
int prevSelectedIndex = arrItems.indexOf(selectedSize);
builder.setSingleChoiceItems(items, prevSelectedIndex,...