按下返回时,EditText不会触发更改

在使用购物车的应用程序中,我可以选择通过仅允许数字输入的EditText来更改项目的数量。
一切正常,除非用户改变字段,然后按后退键隐藏软键盘。 在这种情况下,该字段会显示已更改的值,但我不知道如何检测此更改并对其作出反应。 等待切换到另一个活动不是一个选项。
当用户用“完成”按钮确认时,我可以用“OnEditorActionListener”来处理。 但是后面的钥匙呢?

更新:
事实certificate,使用后退关闭软键盘时,onKeyDown / onBackPressed和编辑字段上的OnKeyListener都不会触发。

我在前段时间写的应用程序中遇到了同样的问题。 现在很失望,但这不是你的问题xD。

事实上,没有选择来跟踪这样的操作,但我发现一个伟大的(或多或少)的解决方案来添加这样的function。 理论上相当简单,但我认为这也是“黑客”。

所以你将需要的是一个自定义的线性布局,其中包含您的应用程序,或一个特殊的区域。 之后,你必须添加一个监听器。 这只能在肖像模式下使用。

所以这里的代码:(我很抱歉,但我不记得来源)

自定义布局:

LinearLayoutThatDetactsSoftwarekeyboard.java (这是布局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添加了一个Listener,每当layoutheight改变至少128个像素时,它将被调用。 这是一个技巧,它不会用于小于128像素的键盘(但我认为每个键盘都有这样的高度)。如果LayoutHeight已经改变,你会得到通知,如果它现在显示或不。

我希望我的回答很有用。 也许你再次在StackOverFlow上find真正的源代码。 我不会偷别人的天才。 学分发给陌生人;)

这是@ mikepenz的答案的修改版本,因为它不适用于Android 8windowSoftInputMode =“adjustPan”模式。

语言是kotlin 。 使用此视图作为根视图并设置keyboardListener:

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

你可以重写你的活动中的onBackPressed(伪代码 – 被警告):

 @Override public void onBackPressed() { if(userHasChangedAnything) { revertChanges(); } else { super.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; } }) 

对不起,如果这是不相关的,但我曾经有这个问题,忘记了我已经添加了一个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; } });