59 lines
1.6 KiB
Kotlin
59 lines
1.6 KiB
Kotlin
package com.sw.dualscreen.utils
|
|
|
|
import android.app.Activity
|
|
import android.content.Context
|
|
import android.view.View
|
|
import android.view.inputmethod.EditorInfo
|
|
import android.view.inputmethod.InputMethodManager
|
|
import android.widget.EditText
|
|
|
|
/**
|
|
* 键盘工具类
|
|
*/
|
|
object KeyboardUtils {
|
|
|
|
/**
|
|
* 显示键盘
|
|
*/
|
|
fun showKeyboard(editText: EditText) {
|
|
editText.requestFocus()
|
|
val imm = editText.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
|
|
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT)
|
|
}
|
|
|
|
/**
|
|
* 隐藏键盘
|
|
*/
|
|
fun hideKeyboard(activity: Activity) {
|
|
val view = activity.currentFocus ?: activity.window.decorView
|
|
hideKeyboard(view)
|
|
}
|
|
|
|
/**
|
|
* 隐藏键盘
|
|
*/
|
|
fun hideKeyboard(view: View) {
|
|
val imm = view.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
|
|
imm.hideSoftInputFromWindow(view.windowToken, 0)
|
|
}
|
|
|
|
/**
|
|
* 为EditText设置EditorAction监听并隐藏键盘
|
|
*/
|
|
fun setupEditorAction(editText: EditText, onAction: (() -> Unit)? = null) {
|
|
editText.setOnEditorActionListener { _, actionId, _ ->
|
|
if (actionId == EditorInfo.IME_ACTION_DONE ||
|
|
actionId == EditorInfo.IME_ACTION_SEARCH ||
|
|
actionId == EditorInfo.IME_ACTION_GO ||
|
|
actionId == EditorInfo.IME_ACTION_SEND ||
|
|
actionId == EditorInfo.IME_ACTION_NEXT) {
|
|
|
|
hideKeyboard(editText)
|
|
onAction?.invoke()
|
|
true
|
|
} else {
|
|
false
|
|
}
|
|
}
|
|
}
|
|
} |