未在最后一个 RadioButton 上设置标志

Flag not set on last RadioButton

我的应用程序使用 onActivityResult 获取一个地址,用该地址生成一个 RadioButton,并在最后一个 radioButton 上设置一个标志。首次启动时它可以工作。我的问题是,在关闭 activity 并重复添加地址的操作后,最后一个 radioButton 上没有设置标志。我试图通过在 Toolbar 中单击 navigationIcon 时调用 clearCheck() 方法来解决此问题,但它没有用

添加地址并设置标志

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if (data == null) {
        return
    }
    val radioButton = RadioButton(this)
    radioButton.text = data.getStringExtra("ADDRESS")

    radioGroupAddress.apply {
        addView(radioButton)
        //Ставлю флаг на последний выбранный адрес
        check(radioGroupAddress.childCount)
    }
}

UPD

我的Activity

@Inject
@InjectPresenter
lateinit var presenter: SupermarketOrderPresenter

@ProvidePresenter
fun providePresenter() = presenter

override fun onCreate(savedInstanceState: Bundle?) {
    App.appComponent
            .supermarketOrderInfo()
            .build()
            .inject(this)

    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_order_supermarket)
    init()
}

private fun init() {
    initToolbar()
    initClickListeners()
}

private fun initToolbar(){
    //Инициализация тулбара
    setSupportActionBar(toolbar)
    supportActionBar?.setDisplayHomeAsUpEnabled(true)
    supportActionBar?.setDisplayShowHomeEnabled(true)
    //Получение тайтла для дальнейших манипуляций
    val titleToolbar = toolbar.getChildAt(0) as TextView
    //Установка тайтла для тулбара
    titleToolbar.text = getString(R.string.total_price_toolbar_order,
            intent.getStringExtra("TOTAL_PRICE"))
    //Установка шрифта для тулбара
    titleToolbar.typeface = ResourcesCompat.getFont(this, R.font.sf_pro_display_bold)
    //Клик на иконку "назад"
    toolbar.setNavigationOnClickListener {
        radioGroupAddress.clearCheck()
        onBackPressed()
    }
}

private fun initClickListeners(){
    buttonAddAddress.setOnClickListener {
        val intent = Intent(this, SupermarketMapActivity::class.java)
        startActivityForResult(intent, 2)
    }

    imageButtonInfoDelivery.setOnClickListener {
        showDialogInfoDelivery()
    }
    buttonPay.setOnClickListener {
        when {
            radioGroupAddress.childCount == 0 -> {
                startActivityForResult(Intent(this, SupermarketMapActivity::class.java), 2)
            }
            editTextName.text.isNullOrEmpty() || maskedEditTextPhone.text!!.length < 18 -> {
                toast("Имя и телефон должны быть заполнены")
            }
            else -> startActivity(Intent(this, OrderSuccessSupermarketActivity::class.java))
        }
    }
}

private fun showDialogInfoDelivery() {
    val dialog = Dialog(this)
    dialog.setContentView(R.layout.custom_dialog_info_delivery)

    val buttonOk = dialog.findViewById<Button>(R.id.buttonDialogInfo)
    buttonOk.setOnClickListener {
        dialog.dismiss()
    }
    dialog.show()
}

//Получаю выбранный на карте адрес
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if (data == null) {
        return
    }
    val radioButton = RadioButton(this)
    radioButton.text = data.getStringExtra("ADDRESS")

    radioGroupAddress.apply {
        addView(radioButton)
        //Ставлю флаг на последний выбранный адрес
        check(radioGroupAddress.childCount)
    }
}

check 方法采用 int id 参数,而不是 View 的位置。在 addView 之后只需使用 radioButton.setChecked(true)/radioButton.checked = true 并且 radioGroupAddress 应该自动取消选中所有其他 RadioButtons(假设它是 RadioGroup