android - Android DialogFragment 视图未更新
问题描述
我正在使用 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")
}
解决方案
尝试view: View?
在您的 DialogFragment 中创建一个变量,然后替换
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()
}
并记住在方法中返回view
变量onCreateView
而不是膨胀另一个视图。重写该onDestroyView
方法以防止内存泄漏:
override fun onDestroyView() {
view = null
super.onDestroyView()
}
此外,R.id.manual_card_view
switch语句中的R.id.manual_radio_button
请注意,启用/禁用卡片视图不会影响子视图,因此仍然可以与文本字段进行交互。
推荐阅读
- types - 我可以从 ocaml 中的匹配中获取 alpha 和 boolean 类型吗?
- java - org.apache.spark.SparkException 任务不可序列化原因:ForeachPartitionFunction 中的 java.io.NotSerializableException
- sql - 计算具有 HH.MM 格式时间的两个 varchar2 列之间的时间差
- unity3d - 在播放模式期间无法更改 Alpha
- ios - 在 viewWillAppear 中更改字符串时自动换行错误
- java - maven 无法将项目方面动态 Web 模块的版本更改为 3.1
- reactjs - 只需使用 action creators、redux 和 thunk 将对象添加到空数组
- android - Google 应用程序 - “保留笔记”即使在设备重启后如何进行提醒?
- reactjs - React 组件 AMD 与 Webpack 捆绑为 Require.js
- android - ionic3 中的指纹身份验证未准备就绪