添加按钮,input搜索公司

This commit is contained in:
manchuwork
2025-10-24 13:36:55 +08:00
parent 44b36b9c77
commit c81724de1a
2 changed files with 114 additions and 14 deletions

View File

@@ -7,12 +7,13 @@ enum class ActionEvent {
SET_DATA_SOURCE("com.loveerror.bested.SET_DATA_SOURCE", "设置数据源"),
HIDE_FLOATING_BUTTON("com.loveerror.bested.HIDE_FLOATING_BUTTON", "隐藏悬浮按钮"),
PRINT_CURRENT_PAGE("com.loveerror.bested.PRINT_CURRENT_PAGE", "打印当前界面"),
QUERY_COMPANY_REGISTER("com.loveerror.bested.QUERY_COMPANY_REGISTER", "查询公司是否注册",isDisplay = true, menuWithText = true),
UNKNOWN("com.loveerror.bested.UNKNOWN", "未知",isDisplay = false);
constructor(event: String, menuName: String, isDisplay: Boolean = true){
constructor(event: String, menuName: String, isDisplay: Boolean = true, menuWithText: Boolean= false){
this.event = event
this.menuName = menuName
this.menuWithText = menuWithText
this.isDisplay = isDisplay
}
@@ -21,6 +22,8 @@ enum class ActionEvent {
val isDisplay: Boolean
val menuWithText: Boolean
companion object {
fun getByMenuName(menuName: String) :ActionEvent {
for (item in ActionEvent.entries){

View File

@@ -1,12 +1,20 @@
package com.loveerror.bested.button
import android.app.AlertDialog
import android.content.Context
import android.graphics.*
import android.text.InputType
import android.text.method.ScrollingMovementMethod
import android.view.Gravity
import android.view.MotionEvent
import android.view.View
import android.view.WindowManager
import android.widget.EditText
import android.widget.PopupMenu
import android.app.Activity
import android.content.ContextWrapper
import android.app.Application
import android.widget.Toast
class DragFloatingButton(context: Context) : View(context) {
private val paint = Paint().apply {
isAntiAlias = true
@@ -121,25 +129,114 @@ class DragFloatingButton(context: Context) : View(context) {
// 添加菜单项 use ActionEvent
for (item in ActionEvent.entries){
if (item.isDisplay) {
popup.menu.add(item.menuName)
// 检查是否需要文本输入
if (item.menuWithText) {
popup.menu.add(0, item.ordinal, 0, item.menuName).setOnMenuItemClickListener {
showTextInputDialog(item)
true
}
} else {
popup.menu.add(0, item.ordinal, 0, item.menuName)
}
// popup.menu.add(item.menuName)
}
}
// 为不需要文本输入的菜单项设置监听器
popup.setOnMenuItemClickListener { item ->
val actionEvent = ActionEvent.getByMenuName(item.title.toString())
try {
context.sendBroadcast(android.content.Intent(actionEvent.event).apply {
setPackage(context.packageName)
})
} catch (e: IllegalArgumentException) {
// 处理枚举值不存在的情况
e.printStackTrace()
println("枚举值不存在: ${item.title},${actionEvent}, ${e.toString()}")
val actionEvent = ActionEvent.entries.find { it.ordinal == item.itemId }
if (actionEvent != null && !actionEvent.menuWithText) {
triggerActionEvent(actionEvent)
}
true
}
popup.show()
// popup.setOnMenuItemClickListener { item ->
// val actionEvent = ActionEvent.getByMenuName(item.title.toString())
// try {
// context.sendBroadcast(android.content.Intent(actionEvent.event).apply {
// setPackage(context.packageName)
// })
// } catch (e: IllegalArgumentException) {
// // 处理枚举值不存在的情况
// e.printStackTrace()
// println("枚举值不存在: ${item.title},${actionEvent}, ${e.toString()}")
// }
// true
// }
//
// popup.show()
}
private fun showTextInputDialog(actionEvent: ActionEvent) {
println("Attempting to show text input dialog for: ${actionEvent.menuName}")
val editText = EditText(context).apply {
inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_FLAG_MULTI_LINE
setLines(5)
gravity = Gravity.TOP or Gravity.START
setTextIsSelectable(true)
movementMethod = ScrollingMovementMethod.getInstance()
}
val dialog = AlertDialog.Builder(context)
.setTitle("请输入公司名称")
.setView(editText)
.setPositiveButton("确定", null) // 先设置为null稍后手动处理
.setNegativeButton("取消", null)
.create()
// 手动处理PositiveButton点击事件
dialog.setOnShowListener {
val positiveButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE)
positiveButton.setOnClickListener {
val inputText = editText.text.toString().trim()
// 如果文本为空,则显示提示并不关闭对话框
if (inputText.isEmpty()) {
// 在EditText下方显示错误提示
editText.error = "请输入公司名称"
// 不关闭对话框
return@setOnClickListener
}
// 输入有效,触发事件并关闭对话框
triggerActionEvent(actionEvent, inputText)
dialog.dismiss()
}
}
// 设置对话框类型为系统级对话框
val window = dialog.window
window?.setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY)
dialog.show()
}
private fun getContextForDialog(): Context? {
var currentContext = context
while (currentContext is ContextWrapper) {
if (currentContext is Activity && !currentContext.isFinishing) {
return currentContext
}
currentContext = currentContext.baseContext
}
return null
}
private fun triggerActionEvent(actionEvent: ActionEvent, textInput: String? = null) {
try {
val intent = android.content.Intent(actionEvent.event).apply {
setPackage(context.packageName)
}
// 如果有文本输入添加到Intent中
textInput?.let {
intent.putExtra("textInput", it)
}
context.sendBroadcast(intent)
} catch (e: IllegalArgumentException) {
e.printStackTrace()
println("枚举值不存在: $actionEvent, ${e.toString()}")
}
}
}