0

大家好,在我的应用程序中,我正在使用 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 值。我如何显示选定的单选按钮,因此当用户再次运行此应用程序时,它会显示选中的中等单选按钮(或其他,如果用户选择其他)。任何建议将不胜感激,在此先感谢

在此处输入图像描述.

4

2 回答 2

2

这就是你想要的:

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();

希望这有帮助!

于 2015-03-20T12:28:44.093 回答
1

selectedSize作为第二个参数传递 给setSingleChoiceItems方法:

ArrayList<CharSequence> arrItems=new ArrayList<CharSequence>(Arrays.asList(items));
int prevSelectedIndex = arrItems.indexOf(selectedSize);
builder.setSingleChoiceItems(items, prevSelectedIndex,...
于 2015-03-20T12:32:49.610 回答