Keyboard hide event with BACK key

后端 未结 5 928
野的像风
野的像风 2020-12-30 13:19

I\'ve noticed in the Android Market Application, when you click over the search button, it shows the keyboard, but when you click the back button,

相关标签:
5条回答
  • 2020-12-30 13:36

    Hey i think the market app is using the googleSearch dialog (check out Searcheable activity ).

    You can implement the editText in a popupWindow, and set the poupwindow as focusable. Show the keyboard when your popup is shown. in onDismiss hide the keyboard.

    popupWindow.setFocusable(true);
    popupWindow.setOnDismissListener(new OnDismissListener() {
    
            @Override
            public void onDismiss() {
                // TODO Auto-generated method stub
                inputMethodManager.hideSoftInputFromWindow(
                            edttxtSearchBar.getWindowToken(), 0);           }
    

    This will ensure, you click anywhere outside popup or press back the popup disappears as well(along with keyboard).

    0 讨论(0)
  • 2020-12-30 13:37
        **perfect answer** REFER THIS **SIMPLE EXAMPLE**...ITS TOOOO GOOOODDDD 
    
                KTBEditTextWithListener.java // Custom edittext
    
                    import android.content.Context;
                    import android.util.AttributeSet;
                    import android.view.KeyEvent;
    
                    public class KTBEditTextWithListener extends android.widget.EditText {
    
                        public KTBEditTextWithListener(Context context) {
                            super(context);
                            // TODO Auto-generated constructor stub
                        }
    
                        public KTBEditTextWithListener(Context context, AttributeSet attrs, int defStyle) {
                            super(context, attrs, defStyle);          
                        //    createFont(context);
                    }
    
                    public KTBEditTextWithListener(Context context, AttributeSet attrs) {
                            super(context, attrs);
                          //  createFont(context);
                    }
    
    
                        private BackPressedListener mOnImeBack;
    
                        /* constructors */
    
                        @Override
                        public boolean onKeyPreIme(int keyCode, KeyEvent event) {
                            if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
                                if (mOnImeBack != null) mOnImeBack.onImeBack(this);
                            }
                            return super.dispatchKeyEvent(event);
                        }
    
                        public void setBackPressedListener(BackPressedListener listener) {
                            mOnImeBack = listener;
                        }
    
                        public interface BackPressedListener {
                            void onImeBack(KTBEditTextWithListener editText);
                        }
                    }
    
    
        //my_layout.xml
                <?xml version="1.0" encoding="utf-8"?>
                <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:orientation="vertical" >
    
                    <com.ktb.gopharma.views.KTBEditTextWithListener
                        android:id="@+id/edit_text"
                        style="@style/match_width">
                        </com.ktb.gopharma.views.KTBEditTextWithListener>
    
                </LinearLayout>
    
        //MyActivity.java
    
                package com.ktb.gopharma;
    
                import android.app.Activity;
                import android.os.Bundle;
                import android.view.View;
                import android.view.View.OnClickListener;
    
                import com.ktb.gopharma.views.KTBEditTextWithListener;
                import com.ktb.gopharma.views.KTBEditTextWithListener.BackPressedListener;
                import com.ktechbeans.gopharma.R;
    
                public class MyActivity extends BaseActivity {
                    @Override
                    protected void onCreate(Bundle savedInstanceState) {
                        super.onCreate(savedInstanceState);
                        setContentView(R.layout.my_layout);
    
                        KTBEditTextWithListener editText = (KTBEditTextWithListener) findViewById(R.id.edit_text);
    
                        editText.setOnClickListener(new OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                showToast("keypad opn");
                            }
                        });
    
                        editText.setBackPressedListener(new BackPressedListener() {
    
                            @Override
                            public void onImeBack(KTBEditTextWithListener editText) {
                                 showToast("keypad close");
                            }
                        });
                    }
    
                }
    
    0 讨论(0)
  • 2020-12-30 13:45

    You need to implement this to capture the BACK button before it is dispatched to the IME:

    http://developer.android.com/reference/android/view/View.html#onKeyPreIme(int, android.view.KeyEvent)

    0 讨论(0)
  • 2020-12-30 13:47

    I think you should handle this using focus:

     final InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    
        edttext.setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(!(hasFocus))
                {   
                mgr.hideSoftInputFromWindow(edttext.getWindowToken(), 0);
                }
    
            }
        });
    
    0 讨论(0)
  • 2020-12-30 13:48

    The google market application is using Fragments via the API Support Package. When you click back it is actually going back in the fragment stack. It's like going back an activity without the screen swipe. The fragment that they go back to does not contain the search box which is why it disappears.

    0 讨论(0)
提交回复
热议问题