Keyboard hide event with BACK key

穿精又带淫゛_ 提交于 2019-11-30 07:31:02

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)

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

        }
    });

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

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.

    **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");
                        }
                    });
                }

            }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!