【问题标题】:Android DialogFragment views not updatingAndroid DialogFragment 视图未更新
【发布时间】:2021-03-23 12:30:35
【问题描述】:

我正在使用 DialogFragment 并调用 onViewCreated,但是卡片视图不会更新其不透明度。

我的代码应该禁用未选中的卡片视图并将其变灰,但没有任何变化。 我尝试将代码放在 onCreateView 中,但效果不佳。

class AddBookDialogFragment: DialogFragment() {
    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        return activity?.let {
            val builder = AlertDialog.Builder(it)
            // Get the layout inflater
            val inflater = requireActivity().layoutInflater

            // Inflate and set the layout for the dialog
            // Pass null as the parent view because its going in the dialog layout
            builder.setView(inflater.inflate(R.layout.dialog_add_book, null))
                // Add action buttons
                .setPositiveButton("Create") { dialog, id ->
                    // create the book
                }
                .setNegativeButton("Cancel") { _, _ ->
                    dialog?.cancel()
                }
            builder.create()
        } ?: throw IllegalStateException("Activity cannot be null")
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val addBookRadioGroup: RadioGroup = view.findViewById(R.id.add_book_radio_group)
        view.findViewById<RadioButton>(R.id.isbn_radio_button).isEnabled = true

        addBookRadioGroup.setOnCheckedChangeListener { _, checkedId ->
            // checkedId is the RadioButton selected

            val manualCardView: CardView = view.findViewById(R.id.manual_card_view)
            val isbnCardView: CardView = view.findViewById(R.id.isbn_card_view)

            var disabledCardView: CardView = manualCardView
            var enabledCardView: CardView = isbnCardView

            // Sets which card view will be enabled / disabled
            when (checkedId) {
                R.id.isbn_radio_button -> {
                    disabledCardView = manualCardView
                    enabledCardView = isbnCardView
                }

                R.id.manual_card_view -> {
                    disabledCardView = isbnCardView
                    enabledCardView = manualCardView
                }
            }

            disabledCardView.isEnabled = false
            // Sets opacity to 60%
            disabledCardView.alpha = 0.6F

            enabledCardView.isEnabled = true
            // Sets to opacity to 100%
            enabledCardView.alpha = 1F
        }
    }

    // On create view must be overridden here so that it does not return null, if it returns null onViewCreated won't run
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return layoutInflater.inflate(R.layout.dialog_add_book, container, false)
    }
}

我的 xml 文件的结构是这样的

<ConstraintLayout>

    <RadioGroup>
        
        <RadioButton>
        <CardView>
            some TextInputEditTexts to input fields
        </CardView>
        
        <RadioButton>
        <CardView>
            some TextInputEditTexts to input fields
        </CardView>
    </RadioGroup>
</ConstraintLayout>

这是我显示 DialogFragment 的方式

activity?.supportFragmentManager?.let { it ->
    AddBookDialogFragment().show(it, "Add Book")
}

【问题讨论】:

    标签: android kotlin dialog


    【解决方案1】:

    尝试在 DialogFragment 中创建一个 view: View? 变量,然后替换

    builder.setView(inflater.inflate(R.layout.dialog_add_book, null))
         .setPositiveButton("Create") { dialog, id ->
         }
         .setNegativeButton("Cancel") { _, _ ->
         dialog?.cancel()
         }
    

    view = inflater.inflate(R.layout.dialog_add_book, null)
    builder.setView(view)
         .setPositiveButton("Create") { dialog, id ->
         }
         .setNegativeButton("Cancel") { _, _ ->
         dialog?.cancel()
         }
    

    并记住在onCreateView 方法中返回view 变量,而不是膨胀另一个视图。 重写 onDestroyView 方法以防止内存泄漏:

    override fun onDestroyView() {
            view = null
            super.onDestroyView()
        }
    

    另外,switch 语句中的R.id.manual_card_view 应该是R.id.manual_radio_button

    请注意,启用/禁用卡片视图不会影响子视图,因此仍然可以与文本字段进行交互。

    【讨论】:

    • 谢谢!我在发布这个答案之前就知道了,但这是完全正确的
    猜你喜欢
    • 1970-01-01
    • 2021-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-25
    • 1970-01-01
    • 1970-01-01
    • 2019-08-25
    相关资源
    最近更新 更多