Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 按back时,EditText不会触发更改_Android_Android Edittext_Back - Fatal编程技术网

Android 按back时,EditText不会触发更改

Android 按back时,EditText不会触发更改,android,android-edittext,back,Android,Android Edittext,Back,在使用购物车的应用程序中,我提供了通过EditText更改项目数量的选项,EditText只允许数字输入。 除了用户更改字段,然后按back键隐藏软键盘外,其他操作都正常。在本例中,该字段显示更改的值,但我不知道如何检测此更改并对其作出反应。等待切换到其他活动不是一个选项。 当用户用“完成”按钮确认时,我可以用“OnEditorActionListener”来处理这个问题。但是后面的钥匙呢 更新: 事实证明,当使用back键关闭软键盘时,编辑字段上的onKeyDown/onBackPressed

在使用购物车的应用程序中,我提供了通过EditText更改项目数量的选项,EditText只允许数字输入。
除了用户更改字段,然后按back键隐藏软键盘外,其他操作都正常。在本例中,该字段显示更改的值,但我不知道如何检测此更改并对其作出反应。等待切换到其他活动不是一个选项。
当用户用“完成”按钮确认时,我可以用“OnEditorActionListener”来处理这个问题。但是后面的钥匙呢

更新:

事实证明,当使用back键关闭软键盘时,编辑字段上的onKeyDown/onBackPressed和OnKeyListener都不会触发。

您可以在活动中覆盖onBackPressed(伪代码-警告):


由于代码的新信息位于片段中,我建议在文本框中添加一个键侦听器。此代码尚未经过测试,但应该可以正常工作

((EditText) findViewById(R.id.button)).setOnKeyListener(new OnKeyListener() { 
    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
         if ((keyCode == KeyEvent.KEYCODE_BACK)) {
              Log.d(this.getClass().getName(), "back button pressed");
              return true;
         }
         return false;
    }
})

我在不久前写的一个应用程序中遇到了同样的问题。现在已经不满足了,但这不是你的问题xD

事实上,没有办法跟踪这样的操作,但我发现了一个很好的(或多或少)解决方案来添加这样的功能。理论上很简单,但我认为这也是一个“黑客”

因此,您需要的是一个自定义线性布局,其中包含您的应用程序,或一个特殊区域。之后,您必须添加一个侦听器。这将只在纵向模式下工作

下面是代码:(很抱歉,我记不起代码的来源)

自定义布局:

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

以及如何添加侦听器:

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添加了一个监听器,每次Layouthweight更改至少128像素时都会调用该监听器。这是一个技巧,对于小于128像素的键盘不起作用(但我认为每个键盘都有这样的高度) 如果Layouthweight已更改,您将收到通知,知道它是否正在显示


我希望我的回答是有用的。也许你在StackOverFlow上找到了真正的来源。我不会偷别人的天才,所以。信用卡归不知名人士所有;)

如果这不相关,请原谅,但我曾经遇到过这个问题,并且忘记了我添加了一个OnKeyListener,它捕捉到了我的dialogFragment的背面按下。 我把听众改写如下。添加了Top条件以解决该问题

getDialog().setOnKeyListener(new DialogInterface.OnKeyListener() {
    @Override
    public boolean onKey(DialogInterface dialogInterface, int i, KeyEvent keyEvent) {
        if(keyEvent.getKeyCode() == KeyEvent.KEYCODE_DEL) {
            return false;
        }
        if(keyEvent.getAction() == KeyEvent.ACTION_DOWN && keyEvent.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            if(iYoutubePickerDialog != null) {
                iYoutubePickerDialog.closeYoutubeDialog(getDialog());
            }
        }
        return true;
    }
});

这是@mikepenz答案的修改版本,因为它在Android 8上无法使用windowSoftInputMode=“adjustPan”模式

语言为kotlin。 将此视图用作根视图并设置键盘侦听器:

class KeyboardAwareConstraintLayout(context: Context?, attrs: AttributeSet?) : ConstraintLayout(context, attrs)
{

  var keyboardListener: KeyboardListener? = null

  constructor(context: Context?) : this(context, null)


  override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int)
  {
    val rect = Rect()
    getWindowVisibleDisplayFrame(rect)
    val statusBarHeight = rect.top
    val keyboardHeight = rootView.height - (rect.bottom - rect.top) - statusBarHeight
    // R.dimen.keyboard_min_height has set to 120dp, 
    // as a reasonable minimum height of a soft keyboard
    val minKeyboardHeight = resources.getDimensionPixelSize(R.dimen.keyboard_min_height)

    keyboardListener?.onKeyboardShown(keyboardHeight > minKeyboardHeight)

    super.onMeasure(widthMeasureSpec, heightMeasureSpec)
  }
}

interface KeyboardListener
{
  fun onKeyboardShown (shown: Boolean)
}

我也考虑过这一点,但是(假设它有效)这将是一个很大的漏洞,因为EditText是在一个片段中,而不是在一个活动中,所以处理这一问题的代码将分散在各个部分。没有更好的解决方案吗?这也不起作用(onKeyDown也不起作用)。当关闭键盘时,它不会启动。我还没有尝试过这个解决方案,但看看代码,我很确定它能工作。我从来没想到这个看似微不足道的问题会需要这样的破解。谢谢你的代码(即使不是自己写的)!
getDialog().setOnKeyListener(new DialogInterface.OnKeyListener() {
    @Override
    public boolean onKey(DialogInterface dialogInterface, int i, KeyEvent keyEvent) {
        if(keyEvent.getKeyCode() == KeyEvent.KEYCODE_DEL) {
            return false;
        }
        if(keyEvent.getAction() == KeyEvent.ACTION_DOWN && keyEvent.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            if(iYoutubePickerDialog != null) {
                iYoutubePickerDialog.closeYoutubeDialog(getDialog());
            }
        }
        return true;
    }
});
class KeyboardAwareConstraintLayout(context: Context?, attrs: AttributeSet?) : ConstraintLayout(context, attrs)
{

  var keyboardListener: KeyboardListener? = null

  constructor(context: Context?) : this(context, null)


  override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int)
  {
    val rect = Rect()
    getWindowVisibleDisplayFrame(rect)
    val statusBarHeight = rect.top
    val keyboardHeight = rootView.height - (rect.bottom - rect.top) - statusBarHeight
    // R.dimen.keyboard_min_height has set to 120dp, 
    // as a reasonable minimum height of a soft keyboard
    val minKeyboardHeight = resources.getDimensionPixelSize(R.dimen.keyboard_min_height)

    keyboardListener?.onKeyboardShown(keyboardHeight > minKeyboardHeight)

    super.onMeasure(widthMeasureSpec, heightMeasureSpec)
  }
}

interface KeyboardListener
{
  fun onKeyboardShown (shown: Boolean)
}