問(wèn)題描述
在帶有購(gòu)物車(chē)的應(yīng)用程序中,我提供了通過(guò) EditText 更改商品數(shù)量的選項(xiàng),該選項(xiàng)只允許輸入數(shù)字.
一切正常,除非用戶(hù)更改字段然后按返回鍵隱藏軟鍵盤(pán).在這種情況下,該字段顯示了更改的值,但我不知道如何檢測(cè)此更改并對(duì)其做出反應(yīng).等待切換到另一個(gè)活動(dòng)不是一種選擇.
當(dāng)用戶(hù)使用完成"按鈕確認(rèn)時(shí),我可以使用OnEditorActionListener"來(lái)處理它.但是后退鍵呢?
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?
更新:
事實(shí)證明,使用返回鍵關(guān)閉軟鍵盤(pán)時(shí),編輯字段上的 onKeyDown/onBackPressed 和 OnKeyListener 都不會(huì)觸發(fā).
update:
As it turned out, neither onKeyDown / onBackPressed nor OnKeyListener on the edit field do trigger when closing the soft keyboard with back key.
推薦答案
我在前段時(shí)間寫(xiě)的一個(gè)應(yīng)用程序中遇到了同樣的問(wèn)題.它現(xiàn)在已停產(chǎn),但這不是你的問(wèn)題 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.
事實(shí)上,沒(méi)有選項(xiàng)可以跟蹤這樣的操作,但我找到了一個(gè)很好的(或多或少)解決方案來(lái)添加這樣的功能.這在理論上很簡(jiǎn)單,但我認(rèn)為它也是一個(gè)黑客".
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.
因此,您需要一個(gè)包含您的應(yīng)用程序或特殊區(qū)域的自定義線(xiàn)性布局.之后,您必須為其添加一個(gè)偵聽(tīng)器.這僅適用于縱向模式.
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.
所以這里是代碼:(對(duì)不起,我不記得出處了)
So here to the code: (i'm sorry but i can't remember the source)
自定義布局:
LinearLayoutThatDetactsSoftwarekeyboard.java(這是布局 xD 的原始名稱(chēng))
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);
}
}
以及如何添加監(jiān)聽(tīng)器:
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 添加了一個(gè)監(jiān)聽(tīng)器,每當(dāng) layoutheight 改變至少 128 像素時(shí)都會(huì)調(diào)用該監(jiān)聽(tīng)器.這是一個(gè)技巧,它不適用于小于 128 像素的鍵盤(pán)(但我認(rèn)為每個(gè)鍵盤(pán)都有這樣的高度)如果 LayoutHeight 已更改,您將收到通知它是否現(xiàn)在顯示.
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 上找到了真正的來(lái)源.所以我不會(huì)偷別人的天才.學(xué)分歸未知人所有;)
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 ;)
這篇關(guān)于按下后退時(shí) EditText 不會(huì)觸發(fā)更改的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!