
在带有购物车的应用程序中,我提供了通过EditText更改项目数量的选项,该文本仅允许数字输入.
一切正常,除非用户更改字段然后按后退键以隐藏软键盘.在这种情况下,该字段显示更改的值,但我不知道如何检测此更改并对其作出反应.等待切换到另一个活动不是一种选择.
当用户使用“完成”按钮确认时,我可以使用“OnEditorActionListener”处理此问题.但是后退键呢?
更新:
事实证明,当使用后退键关闭软键盘时,编辑字段上的onKeyDown / onBackpressed和OnKeyListener都不会触发.
解决方法:
我在一段时间前写的应用程序中遇到了同样的问题.它现在已经不在了,但这不是你的问题xD.
事实上,没有选项来跟踪这样的 *** 作,但我发现了一个很棒的(或多或少)解决方案来添加这样的功能.它在理论上非常简单,但我认为它也是“黑客”.
因此,您需要的是一个自定义线性布局,其中包含您的应用程序或特殊区域.之后你必须添加一个监听器.这只适用于纵向模式.
所以这里的代码:(对不起,但我不记得来源)
自定义布局:
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上找到真正的源代码.我不会这样偷走某人的天才.积分归于未知的人;)
总结以上是内存溢出为你收集整理的android – EditText在按下后退时不会触发更改全部内容,希望文章能够帮你解决android – EditText在按下后退时不会触发更改所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)