我已将targetSdkVersion和compileSdkVersion升级到33。之后,键盘返回事件不会被触发。
根据Android文档,建议迁移到Predictive back手势。我已经尝试使用建议的解决方案来使用OnBackInvokedDispatcher和OnBackPressedCallback,但它们都没有帮助获取键盘返回事件。
// Add onBackPressed as default back behavior.
mDefaultBackCallback = this::onBackPressed;
getOnBackInvokedDispatcher().registerOnBackInvokedCallback(
OnBackInvokedDispatcher.PRIORITY_DEFAULT, mDefaultBackCallback);
mDefaultBackCallback = null;
在更新到Android 13之前,我能够在上获得回调
@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {resetView();}
return super.onKeyPreIme(keyCode, event);
}
在这个问题上有什么帮助吗?
你试过这个吗
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if(event.getKeyCode() == KeyEvent.KEYCODE_BACK){
Log.d("Test","Back Pressed");
return viewModel.backClicked();
}
return super.dispatchKeyEvent(event);
}
我遇到了AppCompatEditText
的实现无法再工作的问题。
我使用的是:
class CustomAppCompatEditText : AppCompatEditText {
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(
context,
attrs,
defStyleAttr
)
override fun onKeyPreIme(keyCode: Int, event: KeyEvent?): Boolean {
if (keyCode == KeyEvent.KEYCODE_BACK) {
clearFocus()
}
return super.onKeyPreIme(keyCode, event)
}
override fun onEditorAction(actionCode: Int) {
if (actionCode == EditorInfo.IME_ACTION_DONE) {
clearFocus()
}
super.onEditorAction(actionCode)
}
}
我创建了一个名为KeyboardHelper
的新类。它看起来像这样:
object KeyboardHelper {
fun View?.clearFocusOnKeyboardHidden() {
var wasOpened = false
val listener = ViewTreeObserver.OnGlobalLayoutListener {
val isOpen = isKeyboardVisible(this@clearFocusOnKeyboardHidden?.context?.getActivity())
if (isOpen == wasOpened) {
// The keyboard state has not changed yet!
return@OnGlobalLayoutListener
}
wasOpened = isOpen
if (this@clearFocusOnKeyboardHidden?.isFocused == true && !isOpen) {
this@clearFocusOnKeyboardHidden.clearFocus()
}
}
this?.viewTreeObserver?.addOnGlobalLayoutListener(listener)
}
private fun isKeyboardVisible(activity: Activity?): Boolean {
activity ?: return false
val outRect = Rect()
val activityRoot = activity.getActivityRoot()
activityRoot.getWindowVisibleDisplayFrame(outRect)
val location = IntArray(2)
activity.getContentRoot().getLocationOnScreen(location)
val screenHeight = activityRoot.rootView.height
val heightDiff = screenHeight - outRect.height() - location[1]
return heightDiff > screenHeight * 0.15
}
private fun Context.getActivity(): Activity? {
return when (this) {
is Activity -> this
is ContextWrapper -> this.baseContext.getActivity()
else -> null
}
}
private fun Activity.getActivityRoot(): View {
return this.getContentRoot().rootView
}
private fun Activity.getContentRoot(): ViewGroup {
return this.findViewById(android.R.id.content)
}
}
现在我对CustomAppCompatEditText
类做了一些更改:
class CustomAppCompatEditText : AppCompatEditText {
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(
context,
attrs,
defStyleAttr
)
init {
clearFocusOnKeyboardHidden()
}
override fun onEditorAction(actionCode: Int) {
if (actionCode == EditorInfo.IME_ACTION_DONE) {
clearFocus()
}
super.onEditorAction(actionCode)
}
}
毕竟,它现在又像以前一样工作了。键盘代码的源代码来自这里:https://github.com/yshrsmz/KeyboardVisibilityEvent