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