选择时警告对话框不会更改

Alert Dialog not Change when selection

我正在尝试在我的应用程序中使用多选语言。当我选择它时语言会改变,但选择不会改变。我使用两种语言,如果我选择第一种语言(印度尼西亚)更改,但如果我选择第二种语言(英语),则选择保留在第一种选择(印度尼西亚)上,并且语言更改为英语。这是我的代码。

 Button changeLang = findViewById(R.id.bahasa);
        changeLang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showChangeLanguageDialog();

            }
        });

此代码用于显示对话框警报。

  private void showChangeLanguageDialog() {
            final String[] listItems = {"Indonesia", "English"};
            AlertDialog.Builder mBuilder = new AlertDialog.Builder(AboutActivity.this);
            mBuilder.setTitle("Pilih Bahasa");
    
    
    
            Integer selectedPos = 0;
            if(listItems.equals("Indonesia")){
                selectedPos = 1; }
    
                    else {
                    selectedPos = 0;
                }
    
            mBuilder.setSingleChoiceItems(listItems, selectedPos, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                String selectedItem = listItems[which];
    
                   if (selectedItem.equals("Indonesia")) {
                        setLocale("in");
                        recreate();
                   } else {
                       setLocale("en");
                       recreate();
                    }
                   dialog.dismiss();
                }
            });
    
            AlertDialog mDialog = mBuilder.create();
            mDialog.show();
        }

private void setLocale(String lang) {

    Locale locale = new Locale(lang);
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());

    SharedPreferences.Editor editor = getSharedPreferences("Settings", MODE_PRIVATE).edit();
    editor.putString("My_Lang", lang);
    editor.apply();
}

public void loadLocale(){
    SharedPreferences preferences = getSharedPreferences("Settings", Activity.MODE_PRIVATE);
    String language = preferences.getString("My_Lang","");
    setLocale(language);
}

看来您设置的语言是正确的,但您的警告对话框并未根据您保存的语言进行设置。您需要通过共享首选项调用语言,而不是调用 listItems。试试下面的代码:

private void showChangeLanguageDialog() {
    final String[] listItems = {"Indonesia", "English"};
    AlertDialog.Builder mBuilder = new AlertDialog.Builder(MainActivity.this);
    mBuilder.setTitle("Pilih Bahasa");

    String language = getSharedPreferences("Settings", Activity.MODE_PRIVATE).getString("My_Lang","");

    Integer selectedPos = 0;

    if(language.equals("en")){
        selectedPos = 1; }
    else {
        selectedPos = 0;
    }

    mBuilder.setSingleChoiceItems(listItems, selectedPos, (dialog, which) -> {
        String selectedItem = listItems[which];

        if (selectedItem.equals("Indonesia")) {
            setLocale("in");
            recreate();
        } else {
            setLocale("en");
            recreate();
        }
        dialog.dismiss();
    });

    AlertDialog mDialog = mBuilder.create();
    mDialog.show();
}