0

こんにちは、私のアプリケーションでは AlertDialog を使用しています。これには 3 つの値があります。ユーザーが 1 つの値を選択すると、選択した値が 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 の場合、selectedSize 値として「Medium」を使用しています。選択したラジオ ボタンを表示する方法。ユーザーがこのアプリケーションを再度実行すると、メディア (ユーザーがその他を選択した場合はその他) のラジオ ボタンが選択済みとして表示されます。どんな提案でも感謝します、前もって感謝します

ここに画像の説明を入力.

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の 2 番目のパラメーターとして次のように渡し ます。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 に答える