From 851eaeb201408e879f9080659ef5b18dedebaecc Mon Sep 17 00:00:00 2001 From: seven332 Date: Sun, 30 Nov 2014 23:01:10 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E6=90=9C=E7=B4=A2=E8=AE=B0?= =?UTF-8?q?=E5=BD=95=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- res/layout/search_normal.xml | 4 +- .../ehviewer/widget/SuggestionHelper.java | 3 +- .../ehviewer/widget/SuggestionTextView.java | 59 +++++++++++++++---- 3 files changed, 53 insertions(+), 13 deletions(-) diff --git a/res/layout/search_normal.xml b/res/layout/search_normal.xml index 1c0b2f077..9f9e5749c 100644 --- a/res/layout/search_normal.xml +++ b/res/layout/search_normal.xml @@ -19,7 +19,8 @@ android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" - android:orientation="vertical" > + android:orientation="vertical" + android:focusableInTouchMode="true" > mAllSuggestions; private ArrayAdapter mAdapte; + private boolean mKeyboardIsShowing = true; + public SuggestionTextView(Context context) { super(context); } @@ -43,14 +49,6 @@ public class SuggestionTextView extends PrefixEditText { super(context, attrs, defStyle); } - /** - * We override this method to avoid replacing the query box text when a - * suggestion is clicked. - */ - @Override - protected void replaceText(CharSequence text) { - } - /** * We override this method to avoid an extra onItemClick being called on * the drop-down's OnItemClickListener by @@ -70,11 +68,52 @@ public class SuggestionTextView extends PrefixEditText { return true; } + @Override + public boolean onTouchEvent(MotionEvent event) { + if (!isPopupShowing()) + showDropDown(); + + return super.onTouchEvent(event); + } + + @Override + public boolean onKeyPreIme(int keyCode, KeyEvent event) { + if (keyCode == KeyEvent.KEYCODE_BACK) { + // special case for the back key, we do not even try to send it + // to the drop down list but instead, consume it immediately + if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) { + KeyEvent.DispatcherState state = getKeyDispatcherState(); + if (state != null) { + state.startTracking(event, this); + } + return true; + } else if (event.getAction() == KeyEvent.ACTION_UP) { + KeyEvent.DispatcherState state = getKeyDispatcherState(); + if (state != null) { + state.handleUpEvent(event); + } + if (event.isTracking() && !event.isCanceled()) { + InputMethodManager imm = (InputMethodManager) + getContext().getSystemService(Context.INPUT_METHOD_SERVICE); + if (imm != null && mKeyboardIsShowing) { + imm.hideSoftInputFromWindow(getWindowToken(), 0); + mKeyboardIsShowing = false; + } else { + clearFocus(); + mKeyboardIsShowing = true; + } + return true; + } + } + } + return super.onKeyPreIme(keyCode, event); + } + public void setSuggestionHelper(SuggestionHelper helper) { if (helper != null) { mAllSuggestions = helper.getQueries(); - mAdapte = new ArrayAdapter(getContext(), - android.R.layout.simple_list_item_1, mAllSuggestions); + mAdapte = new ArrayAdapter(ViewUtils.sContextThemeWrapper, + android.R.layout.simple_dropdown_item_1line, mAllSuggestions); setAdapter(mAdapte); } else { mAllSuggestions = null;