键盘隐藏事件与BACK键

时间:2011-10-03 11:27:46

标签: android android-virtual-keyboard

我在Android Market Application中注意到,当您点击搜索按钮时,它会显示键盘,但是当您点击back按钮时,搜索EditText将变为不可见, keyboard被隐藏了。问题是在按下后退键后隐藏键盘后我无法隐藏EditText,因为我找不到用于隐藏键盘事件的侦听器。 我发现了这个示例How to capture the "virtual keyboard show/hide" event in Android? 但它在软键盘上不起作用。

5 个答案:

答案 0 :(得分:21)

您需要实现此操作以捕获BACK按钮,然后再将其分派到IME:

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

答案 1 :(得分:3)

我认为您应该使用焦点来处理这个问题:

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

        }
    });

答案 2 :(得分:0)

嘿,我认为市场应用程序正在使用googleSearch对话框(请查看Searcheable activity)。

您可以在popupWindow中实现editText,并将poupwindow设置为focusable。显示弹出窗口时显示键盘。在onDismiss中隐藏键盘。

popupWindow.setFocusable(true);
popupWindow.setOnDismissListener(new OnDismissListener() {

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

这将确保您点击弹出窗口外的任何地方或按回弹出窗口也会消失(与键盘一起)。

答案 3 :(得分:0)

谷歌市场应用程序正在通过API支持包使用Fragments。当你单击它时它实际上回到了片段堆栈中。这就像是在没有屏幕滑动的情况下返回活动。他们返回的片段不包含搜索框,这就是它消失的原因。

答案 4 :(得分:-2)

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

            }
相关问题