设置
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
package com.example.defenseapplication.view
|
||||
|
||||
import android.app.Dialog
|
||||
import android.content.DialogInterface
|
||||
import android.os.Bundle
|
||||
import android.view.Gravity
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.view.WindowManager
|
||||
import androidx.fragment.app.DialogFragment
|
||||
import com.example.defenseapplication.R
|
||||
import com.example.defenseapplication.databinding.DialogCustomBinding
|
||||
|
||||
class CustomDialogFragmentText : DialogFragment() {
|
||||
|
||||
private lateinit var binding: DialogCustomBinding
|
||||
private var title: String = ""
|
||||
private var message: String = ""
|
||||
private var inputHint: String = ""
|
||||
private var inputText: String = ""
|
||||
private var negativeText: String = "取消"
|
||||
private var positiveText: String = "确定"
|
||||
private var showInput: Boolean = true
|
||||
private var onConfirmListener: ((String) -> Unit)? = null
|
||||
|
||||
fun setTitle(title: String): CustomDialogFragmentText {
|
||||
this.title = title
|
||||
return this
|
||||
}
|
||||
|
||||
fun setMessage(message: String): CustomDialogFragmentText {
|
||||
this.message = message
|
||||
return this
|
||||
}
|
||||
|
||||
fun setInputHint(hint: String): CustomDialogFragmentText {
|
||||
this.inputHint = hint
|
||||
return this
|
||||
}
|
||||
|
||||
fun setInputText(text: String): CustomDialogFragmentText {
|
||||
this.inputText = text
|
||||
return this
|
||||
}
|
||||
|
||||
fun setNegativeText(text: String): CustomDialogFragmentText {
|
||||
this.negativeText = text
|
||||
return this
|
||||
}
|
||||
|
||||
fun setPositiveText(text: String): CustomDialogFragmentText {
|
||||
this.positiveText = text
|
||||
return this
|
||||
}
|
||||
|
||||
fun setShowInput(show: Boolean): CustomDialogFragmentText {
|
||||
this.showInput = show
|
||||
return this
|
||||
}
|
||||
|
||||
fun setOnConfirmListener(listener: (String) -> Unit) {
|
||||
this.onConfirmListener = listener
|
||||
}
|
||||
|
||||
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
|
||||
val dialog = super.onCreateDialog(savedInstanceState)
|
||||
dialog.window?.let {
|
||||
val params = it.attributes
|
||||
params.gravity = Gravity.CENTER
|
||||
params.width = WindowManager.LayoutParams.MATCH_PARENT
|
||||
params.height = WindowManager.LayoutParams.WRAP_CONTENT
|
||||
it.attributes = params
|
||||
it.setBackgroundDrawableResource(R.drawable.round_bg)
|
||||
}
|
||||
return dialog
|
||||
}
|
||||
|
||||
override fun onCreateView(
|
||||
inflater: LayoutInflater,
|
||||
container: ViewGroup?,
|
||||
savedInstanceState: Bundle?
|
||||
): View {
|
||||
binding = DialogCustomBinding.inflate(inflater, container, false)
|
||||
return binding.root
|
||||
}
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
binding.tvTitle.text = title
|
||||
binding.tvMessage.text = message
|
||||
binding.etInput.hint = inputHint
|
||||
binding.etInput.setText(inputText)
|
||||
binding.btnNegative.text = negativeText
|
||||
binding.btnPositive.text = positiveText
|
||||
|
||||
binding.etInput.visibility = if (showInput) View.VISIBLE else View.GONE
|
||||
|
||||
binding.btnNegative.setOnClickListener {
|
||||
dismiss()
|
||||
}
|
||||
|
||||
binding.btnPositive.setOnClickListener {
|
||||
val inputText = binding.etInput.text.toString()
|
||||
onConfirmListener?.invoke(inputText)
|
||||
dismiss()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onDismiss(dialog: DialogInterface) {
|
||||
super.onDismiss(dialog)
|
||||
onConfirmListener = null
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package com.example.defenseapplication.view
|
||||
|
||||
import android.app.Dialog
|
||||
import android.content.Context
|
||||
import android.os.Bundle
|
||||
import android.view.Gravity
|
||||
import android.view.WindowManager
|
||||
import android.widget.LinearLayout
|
||||
import com.example.defenseapplication.R
|
||||
import com.example.defenseapplication.databinding.DialogRecognitionModeBinding
|
||||
|
||||
class RecognitionModeDialog(context: Context) : Dialog(context) {
|
||||
|
||||
private lateinit var binding: DialogRecognitionModeBinding
|
||||
private var selectedMode: String = ""
|
||||
private var onConfirmListener: ((String) -> Unit)? = null
|
||||
|
||||
fun setOnConfirmListener(listener: (String) -> Unit) {
|
||||
this.onConfirmListener = listener
|
||||
}
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
binding = DialogRecognitionModeBinding.inflate(layoutInflater)
|
||||
setContentView(binding.root)
|
||||
|
||||
val window = window
|
||||
window?.let {
|
||||
val params = it.attributes
|
||||
params.gravity = Gravity.BOTTOM
|
||||
params.width = WindowManager.LayoutParams.MATCH_PARENT
|
||||
params.height = WindowManager.LayoutParams.WRAP_CONTENT
|
||||
it.attributes = params
|
||||
it.setBackgroundDrawableResource(R.drawable.round_bg)
|
||||
}
|
||||
|
||||
binding.tvCancel.setOnClickListener {
|
||||
dismiss()
|
||||
}
|
||||
|
||||
binding.itemBluetooth.setOnClickListener {
|
||||
selectedMode = "蓝牙识别"
|
||||
updateSelectedBackground(binding.itemBluetooth)
|
||||
}
|
||||
|
||||
binding.itemBluetoothFace.setOnClickListener {
|
||||
selectedMode = "蓝牙+人脸识别"
|
||||
updateSelectedBackground(binding.itemBluetoothFace)
|
||||
}
|
||||
|
||||
binding.tvConfirm.setOnClickListener {
|
||||
if (selectedMode.isNotEmpty()) {
|
||||
onConfirmListener?.invoke(selectedMode)
|
||||
}
|
||||
dismiss()
|
||||
}
|
||||
}
|
||||
|
||||
private fun updateSelectedBackground(selectedItem: LinearLayout) {
|
||||
binding.itemBluetooth.setBackgroundColor(
|
||||
if (selectedItem == binding.itemBluetooth) {
|
||||
context.resources.getColor(R.color.bg_gray)
|
||||
} else {
|
||||
context.resources.getColor(R.color.white)
|
||||
}
|
||||
)
|
||||
binding.itemBluetoothFace.setBackgroundColor(
|
||||
if (selectedItem == binding.itemBluetoothFace) {
|
||||
context.resources.getColor(R.color.bg_gray)
|
||||
} else {
|
||||
context.resources.getColor(R.color.white)
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user