当我单击 AlertDialog 项目时,关闭并再次打开警报对话框时,单选组不会保持打开状态

When I click on AlertDialog item, the radio group not stay ON when close and open alert dialog again

我希望在单击提醒对话框中的某个项目时 Radiogroup 保持打开状态 (My AlertDialog) 当我再次关闭并打开 alertdialog 时,它仍然保持打开状态 (Like This)

我还使用 [checkeditem: 0] 来保持 radiogroup 始终打开,只是为了 arrayof("English")。 但我不想一直在英文项目上,我想通过选择每个数组来改变它

这是我的代码: 1- MainActivity.kt:

import android.content.DialogInterface
import android.content.res.Configuration
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import java.util.*

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    loadLocale()
    setContentView(R.layout.activity_main)
    val actionBar = supportActionBar!!
    actionBar.title = resources.getString(R.string.app_name)
    val changeLang = findViewById<Button>(R.id.changeMyLang)
    changeLang.setOnClickListener { showChangeLanguageDialog() }
}

private fun showChangeLanguageDialog() {
    val mBuilder = AlertDialog.Builder(this@MainActivity)
    val listItems = arrayOf("English", "فارسی")
    mBuilder.setSingleChoiceItems(listItems, checkeditem: 0) { dialogInterface: DialogInterface, i: Int ->
        if (i == 0) {
            setLocale("en")
            recreate()
        } else if (i == 1) {
            setLocale("fa")
            recreate()
        }
        dialogInterface.dismiss()
    }
    val mDialog = mBuilder.create()
    mDialog.show()
}

private fun setLocale(lang: String?) {
    val locale = Locale(lang)
    Locale.setDefault(locale)
    val config = Configuration()
    config.locale = locale
    baseContext.resources.updateConfiguration(config, baseContext.resources.displayMetrics)
    val editor = getSharedPreferences("Settings", MODE_PRIVATE).edit()
    editor.putString("My_Lang", lang)
    editor.apply()
}

private fun loadLocale() {
    val prefs = getSharedPreferences("Settings", MODE_PRIVATE)
    val language = prefs.getString("My_Lang", "")
    setLocale(language)
}
}

2- activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/welcome"
    android:textSize="30sp" />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/email" />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/password" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/login" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/signup" />

<Button
    android:id="@+id/changeMyLang"
    android:layout_marginTop="20dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/login" />

</LinearLayout>

我使用了 dialogInterface.dismiss() 和 dialogInterface.cancel(),但其中 none 使无线电组保持打开状态。 感谢你们对我的帮助。保重。

将您的方法替换为以下方法,

private fun showChangeLanguageDialog() {
            val pref = getSharedPreferences("Settings", MODE_PRIVATE)
    
            val mBuilder = AlertDialog.Builder(this@MainActivity)
            val listItems = arrayOf("English", "فارسی")
    
            var checkedItem = 0
            if (pref.getString("My_Lang", "en").equals("en")) {
                checkedItem = 0
            } else if (pref.getString("My_Lang", "en").equals("fa")) {
                checkedItem = 1
            }
    
            mBuilder.setSingleChoiceItems(listItems, checkedItem) { dialogInterface: DialogInterface, i: Int ->
                if (i == 0) {
                    setLocale("en")
                    recreate()
                } else if (i == 1) {
                    setLocale("fa")
                    recreate()
                }
                dialogInterface.dismiss()
            }
            val mDialog = mBuilder.create()
            mDialog.show()
    }