按下后退时 EditText 不会触发更改 [英] EditText does not trigger changes when back is pressed

查看:16
本文介绍了按下后退时 EditText 不会触发更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在带有购物车的应用程序中,我提供了通过 EditText 更改商品数量的选项,该选项只允许输入数字.
一切正常,除非用户更改字段然后按返回键隐藏软键盘.在这种情况下,该字段显示了更改的值,但我不知道如何检测此更改并对其做出反应.等待切换到另一个活动不是一种选择.
当用户使用完成"按钮确认时,我可以使用OnEditorActionListener"来处理它.但是后退键呢?

In an application with shopping cart, I give the option to change the amount of items via an EditText which allows only numerical input.
Everything works fine, except when the user changes the field and then presses the back key to hide the soft keyboard. In this case, the field shows the changed value, but I don't know how I can detect this change and react on it. Waiting for a switch to another activity is not an option.
When the user confirms with "done" button, I can handle this with a "OnEditorActionListener". But what about the back key?

更新:
事实证明,使用返回键关闭软键盘时,编辑字段上的 onKeyDown/onBackPressed 和 OnKeyListener 都不会触发.

update:
As it turned out, neither onKeyDown / onBackPressed nor OnKeyListener on the edit field do trigger when closing the soft keyboard with back key.

推荐答案

我在前段时间写的一个应用程序中遇到了同样的问题.它现在已停产,但这不是你的问题 xD.

I had the same problem in an application which i wrote some time ago. It is discontiuned now but that's not your question xD.

事实上,没有选项可以跟踪这样的操作,但我找到了一个很好的(或多或少)解决方案来添加这样的功能.这在理论上很简单,但我认为它也是一个黑客".

In fact there is no option to track such a operation, but i've found a great (more or less) solution to add such a functionality. It's quite simple in theory but it's a "hack" too i think.

因此,您需要一个包含您的应用程序或特殊区域的自定义线性布局.之后,您必须为其添加一个侦听器.这仅适用于纵向模式.

So what you will need is a custom linear layout which contains your application, or a special region. After that you have to add it a listener. And this will work only in portrait mode.

所以这里是代码:(对不起,我不记得出处了)

So here to the code: (i'm sorry but i can't remember the source)

自定义布局:

LinearLayoutThatDetactsSoftwarekeyboard.java(这是布局 xD 的原始名称)

LinearLayoutThatDetactsSoftwarekeyboard.java (that's the original name of the layout xD)

package com.tundem.people.Layout;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.LinearLayout;

/*
 * LinearLayoutThatDetectsSoftKeyboard - a variant of LinearLayout that can detect when 
 * the soft keyboard is shown and hidden (something Android can't tell you, weirdly). 
 */

public class LinearLayoutThatDetectSoftkeyboard extends LinearLayout {

    public LinearLayoutThatDetectSoftkeyboard(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public interface Listener {
        public void onSoftKeyboardShown(boolean isShowing);
    }

    private Listener listener;

    public void setListener(Listener listener) {
        this.listener = listener;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int height = MeasureSpec.getSize(heightMeasureSpec);
        Activity activity = (Activity) getContext();
        Rect rect = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
        int statusBarHeight = rect.top;
        int screenHeight = activity.getWindowManager().getDefaultDisplay().getHeight();
        int diff = (screenHeight - statusBarHeight) - height;
        if (listener != null) {
            listener.onSoftKeyboardShown(diff > 128); // assume all soft
                                                        // keyboards are at
                                                        // least 128 pixels high
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

}

以及如何添加监听器:

final LinearLayoutThatDetectSoftkeyboard lltodetectsoftkeyboard = (LinearLayoutThatDetectSoftkeyboard) findViewById(R.id.LinearLayout_SoftKeyboard);
    lltodetectsoftkeyboard.setListener(new Listener() {

        public void onSoftKeyboardShown(boolean isShowing) {
            if (actMode == MODE_SMS && isShowing) {
                findViewById(R.id.LinearLayout_bottomnavigationbar).setVisibility(View.GONE);
            } else {
                findViewById(R.id.LinearLayout_bottomnavigationbar).setVisibility(View.VISIBLE);
            }
        }
    });

LinearLayout 添加了一个监听器,每当 layoutheight 改变至少 128 像素时都会调用该监听器.这是一个技巧,它不适用于小于 128 像素的键盘(但我认为每个键盘都有这样的高度)如果 LayoutHeight 已更改,您将收到通知它是否现在显示.

The LinearLayout adds a Listener which will be called each time the layoutheight changes by at least 128 pixels. It's a trick and it won't work with keyboards that are smaller than 128 pixels (but i think each keyboard has such a height) If the LayoutHeight has Changed you will get notified if it's showing now or not.

希望我的回答有用.也许您再次在 StackOverFlow 上找到了真正的来源.所以我不会偷别人的天才.学分归未知人所有;)

I hope my answer was useful. Perhaps you find the real source here on StackOverFlow again. I won't steal someones genius so. Credits goes to the unknown person ;)

这篇关于按下后退时 EditText 不会触发更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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