开发者社区> 问答> 正文

关于android PopupWindow点击EditText后返回键无法关闭。

大神们,快来看看这个神奇的问题吧!!
我在Activity弹出一个PopupWindow,PopupWindow上有一个EditText,如果我不去触碰它,我点击返回键可以把PopupWindow关闭,但是我一旦点了输入框,弹出软键盘,那这时我再怎么点返回键都没用,都关不了PopupWindow。以下是我的代码:
screenshot

展开
收起
爵霸 2016-03-13 11:44:02 3959 0
1 条回答
写回答
取消 提交回答
  • 事件被分发到edittext了,为edittext 设置onKeyListener即可,可以参照一下以下代码。

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.Gravity;
    import android.view.KeyEvent;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.View.OnKeyListener;
    import android.view.WindowManager;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.PopupWindow;
    
    public class PopUpWindowTestActivity extends Activity {
    
        private Button btnPopup;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_pop_up_window_test);
    
            btnPopup = (Button) findViewById(R.id.btn_popup);
    
            btnPopup.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    showPopupWindow(v);
                }
            });
    
        }
    
        private MPopupWindow popupWindow = null;
    
        protected void showPopupWindow(View v) {
            if (popupWindow == null)
                popupWindow = new MPopupWindow(this, 500, 300);
            popupWindow.show(v);
        }
    
        class MPopupWindow extends PopupWindow {
    
            private final Activity mContext;
    
            private View contentView;
    
            public MPopupWindow(Activity ctx, int width, int height) {
                super(width, height);
                this.mContext = ctx;
    
                contentView = LayoutInflater.from(mContext).inflate(
                        R.layout.popup_content, null);
                init();
            }
    
            private EditText txtTest;
    
            private void init() {
                txtTest = (EditText) contentView.findViewById(R.id.editText1);
                contentView.setFocusable(true);
                contentView.setFocusableInTouchMode(true);
                this.setFocusable(true);
                this.setOutsideTouchable(true);
                this.setContentView(contentView);
                this.setInputMethodMode(PopupWindow.INPUT_METHOD_FROM_FOCUSABLE);
                this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
                txtTest.setOnKeyListener(new OnKeyListener() {
    
                    @Override
                    public boolean onKey(View v, int keyCode, KeyEvent event) {
                        // TODO Auto-generated method stub
                        if (keyCode == KeyEvent.KEYCODE_BACK) {
                            dismiss();
                            return true;
                        }
                        return false;
                    }
                });
            }
    
    
    
            public void show(View parent) {
                this.setBackgroundAplha(0.4f);
                this.showAtLocation(parent, Gravity.CENTER, 0, 0);
            }
    
            @Override
            public void dismiss() {
                setBackgroundAplha(1.0f);
                super.dismiss();
            }
    
            private void setBackgroundAplha(float alpha) {
                WindowManager.LayoutParams lp = mContext.getWindow()
                        .getAttributes();
                lp.alpha = alpha;
                mContext.getWindow().setAttributes(lp);
            }
    
        }
    
    }
    2019-07-17 19:02:22
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载