按下后强制 EditText 失去焦点 [英] Force EditText to lose focus when back pressed

查看:20
本文介绍了按下后强制 EditText 失去焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户按下后退按钮隐藏键盘时,我试图强制 EditText 控件失去焦点.已经有很多类似的问题了,但几个小时后,我还是没能解决.

I'm trying to force the EditText control to lose focus when the user presses the back button to hide the keyboard. There are many questions similar to this already, but after several hours, I haven't been able to make it work.

首先,只是一点上下文.我有一个带有自定义项目的 ListView.每一项都有多个 TextView 和一个 EditText.我有一个 AfterTextChanged() 方法保存编辑的值.如果有焦点,我设置了一个样式来突出显示该字段.不幸的是,现在更明显的是,当您隐藏(软)键盘时,EditText 实际上并没有失去焦点,我认为这很令人困惑.如果没有键盘,我希望 EditText 不会被聚焦.

First, just a little bit of context. I have a ListView with custom items. Each item has several TextViews and one EditText. I have an AfterTextChanged() method saving edited values. I have a style set up to highlight the field if it has focus. Unfortunately, it is now much more obvious that the EditText doesn't actually lose focus when you hide the (soft) keyboard, and I think it's confusing. I would like the EditText to not be focused if there's no keyboard.

似乎最合理的解决方案是在活动中覆盖 OnBackPressed(),如

The solution that seemed the most reasonable is to override OnBackPressed() in the activity as described here. Unfortunately, it doesn't appear that my method is being called. I.e. the field is still focused, and a breakpoint in the function doesn't fire.

同样,活动上的 OnKeyUp() 侦听器不会触发,而且 Xamarin 似乎不支持 EditText 控件的 OnKeyUp 处理程序.

Similarly, an OnKeyUp() listener on the activity doesn't fire, and Xamarin doesn't appear to support the OnKeyUp handler for the EditText control.

我不想在创建时抑制键盘或其他任何东西,因此使用任何隐形控制技巧也无济于事.

I'm not trying to suppress the keyboard on creation, or anything, so using any of the invisible control tricks don't help either.

很明显,很多人都有这个问题.我相信你们中的一个人已经解决了它!你能分享一下你的解决方案吗?

It's obvious that a lot of people have this problem. I'm sure one of you has solved it! Can you please share your solution?

非常感谢!-凯伦

附言我不需要知道如何隐藏键盘.当用户使用后退按钮隐藏键盘时,我需要采取行动.谢谢:)

P.S. I do not need to know how to hide the keyboard. I need to take an action when the user hides the keyboard with the back button. Thanks :)

推荐答案

根据我的经验,按后退按钮关闭键盘时,onBackPressed()(至少是活动中的默认@Override)通常不会触发.据我所知,它只会在 Back press 启动当前活动的 finish() 时触发.

In my experience onBackPressed() (at least the default @Override one in an activity) will not normally fire when pushing the back button to close the keyboard. As far as I know it will only fire when a Back press would initiate a finish() on the current activity.

下面是一种通过监视视图大小的变化来了解何时显示/隐藏键盘的hacky"方式.您还必须在 AndroidManifest.xml 中将 Activity 设置为 android:windowSoftInputMode="adjustResize".

Below is a kind of "hacky" way to know when the keyboard is shown/hidden by monitoring the change in the view size. You must also set the Activity to android:windowSoftInputMode="adjustResize" in the AndroidManifest.xml.

final View activityRootView = findViewById("Your main View");
    activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        Rect r = new Rect();
        //r will be populated with the coordinates of your view that area still visible.
        activityRootView.getWindowVisibleDisplayFrame(r);

        int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);
        if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
          //Keyboard is shown


        }
        if(heightDiff <= 100) {
            //Keybaord not shown
        }
     }
    });

这篇关于按下后强制 EditText 失去焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆