我为我的Alert Dialog实现了一个“不要再次显示”选项,但它不像预期的那样工作:所有其他的问题都是6-7岁的,都是用java编写的,而且由于我对编程完全陌生,我现在只知道kotlin的基本知识。
fun showDialog() {
val dialogBuilder = AlertDialog.Builder(context)
val inflater = requireActivity().layoutInflater;
dialogBuilder.setView(inflater.inflate(R.layout.alert, null))
dialogBuilder.setMessage("Please always put the total on the bottom right corner. An example is shown below.")
dialogBuilder.setPositiveButton("Alright, got it!",
DialogInterface.OnClickListener { dialog, whichButton ->
pb.visibility = View.VISIBLE
checkPermission(Manifest.permission.CAMERA,CAMERA_PERMISSION_CODE)
startActivityForResult(receiptsViewModel.cameraIntent(requireActivity()),REQUEST_CODE_KAMERA)
})
val mainView: View = inflater.inflate(R.layout.alert, null)
checkBox = mainView.findViewById<View>(R.id.checkBox) as CheckBox
val b = dialogBuilder.create()
b.show()
checkBox.setOnCheckedChangeListener { compoundButton, b ->
if (compoundButton.isChecked) {
storeDialogStatus(true)
} else {
storeDialogStatus(false)
}
}
if (dialogStatus) {
b.hide()
} else {
b.show()
}
}
private fun storeDialogStatus(isChecked: Boolean) {
val mSharedPreferences = requireActivity().getSharedPreferences("CheckItem", AppCompatActivity.MODE_PRIVATE)
val mEditor = mSharedPreferences.edit()
mEditor.putBoolean("item", isChecked)
mEditor.apply()
}
private val dialogStatus: Boolean
private get() {
val mSharedPreferences = requireActivity().getSharedPreferences("CheckItem",
AppCompatActivity.MODE_PRIVATE
)
return mSharedPreferences.getBoolean("item", false)
}发布于 2021-06-08 22:35:57
我看到的问题是,您要向其中添加侦听器的复选框用于不使用的布局。您膨胀了一个布局,并将其设置为对话框视图。然后膨胀布局的第二个副本,并在未使用的第二个布局中在复选框上设置一个侦听器。
一些其他的提示,但这些并不是阻碍它发挥作用的东西。他们只会让你的代码更清晰:
您可以连锁Builder调用,这样就不必一直放置dialogBuilder.,甚至不必将其存储在变量中。
findViewById<View>可以更改为findViewById<CheckBox>,因此不必将其结果转换为CheckBox。
如果不需要对话框,您可以尽早从函数返回,而不是创建对话框,显示它,然后立即隐藏它。
您可以简化为if (someBoolean) doSomething(true) else doSomething(false),而不是使用doSomething(someBoolean)。
private fun startCamera() {
pb.visibility = View.VISIBLE
checkPermission(Manifest.permission.CAMERA,CAMERA_PERMISSION_CODE)
startActivityForResult(
receiptsViewModel.cameraIntent(requireActivity()),
REQUEST_CODE_KAMERA
)
}
fun showDialog() {
if (dialogStatus) {
startCamera()
return
}
val mainView = requireActivity().layoutInflater.inflate(R.layout.alert, null)
checkBox = mainView.findViewById<CheckBox>(R.id.checkBox)
checkBox.setOnCheckedChangeListener { compoundButton, b ->
storeDialogStatus(compoundButton.isChecked)
}
AlertDialog.Builder(context)
.setView(mainView)
.setMessage("Please always put the total on the bottom right corner. An example is shown below.")
.setPositiveButton("Alright, got it!") { _, _ -> startCamera() }
.create()
.show()
}我还发现奇怪的是,您有一个checkBox属性,它在此函数之外不可能有用。你应该把财产移走。
另外,您应该考虑使用DialogFragment而不是普通的对话框。裸对话框有一些问题,比如屏幕旋转后就消失了。官方文档解释了如何使用DialogFragment。不过,我承认,我认为DialogFragment的使用有点费解,特别是对于一个新的安卓程序员。
发布于 2021-06-08 20:50:55
你的逻辑似乎有问题。这段代码不像预期的那样工作:
if (dialogStatus) {
b.hide()
} else {
b.show()
}如果用户选择退出,则不应该创建对话框。请尝试以下修改的代码:
fun showDialog() {
if (dialogStatus) {
return
}
val dialogBuilder = AlertDialog.Builder(context)
val inflater = requireActivity().layoutInflater
dialogBuilder.setView(inflater.inflate(R.layout.alert, null))
.setMessage("Please always put the total on the bottom right corner. An example is shown below.")
.setPositiveButton("Alright, got it!",
DialogInterface.OnClickListener { dialog, whichButton ->
pb.visibility = View.VISIBLE
checkPermission(Manifest.permission.CAMERA, CAMERA_PERMISSION_CODE)
startActivityForResult(
receiptsViewModel.cameraIntent(requireActivity()),
REQUEST_CODE_KAMERA
)
})
dialogBuilder.create().show()
val mainView: View = inflater.inflate(R.layout.alert, null)
checkBox = mainView.findViewById<CheckBox>(R.id.checkBox)
checkBox.setOnCheckedChangeListener { compoundButton, b ->
if (compoundButton.isChecked) {
storeDialogStatus(true)
} else {
storeDialogStatus(false)
}
}
}https://stackoverflow.com/questions/67893045
复制相似问题