力的EditText失去焦点的时候回到pressed [英] Force EditText to lose focus when back pressed

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

问题描述

我试图迫使控制的EditText当用户presses后退按钮隐藏键盘失去焦点。类似的还有这许多问题了,但几个小时后,我一直无法使它发挥作用。

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。每个项目都有几个TextViews和一个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.

这似乎是最合理的解决方案是覆盖OnBack pressed()中的活性如所描述的此处。不幸的是,它不会出现我的方法将被调用。即该领域仍专注,并且在函数中设置断点不火。

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.

我并不想用晚餐preSS键盘上的创作,或任何东西,所以使用任何无形的控制技巧,也没有帮助。

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?

感谢你了!
-Karen

Thank you so much! -Karen

P.S。我并不需要知道如何隐藏键盘。我需要在用户隐藏键盘的后退按钮采取行动。感谢:)

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 :)

推荐答案

在我的经验onBack pressed()(至少是默认@Override之一的活动),通常不会火的时候,推背按钮关闭键盘。据我所知,这只会火当返回preSS将启动完成()在当前活动。

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.

下面是一种哈克的方式时,将显示/通过监控视图大小的变化隐藏在键盘就知道了。您还必须将活动设置为的android:windowSoftInputMode在AndroidManifest.xml =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失去焦点的时候回到pressed的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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