2 zym android zym_android 于 2016.02.18 10:46 提问

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

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

6个回答

a774057695
a774057695   2016.02.18 15:44

好了,这个问题正如我上面说的那样,事件被分发到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);
        }

    }

}

pwz7689
pwz7689 被这个问题困扰了很多天,确实是被文本框获取了焦点导致没法关闭弹窗。谢谢楼主。
一年多之前 回复
juechang123
juechang123 你这个方法不行啊,多试几次你就可能发现问题,有些时候能成功,有些时候不能成功,
一年多之前 回复
lxk_1993
lxk_1993   Rxr 2016.02.18 11:17

把contentview 那三行删掉 试试

a774057695
a774057695   2016.02.18 14:57

问题应该出在这里:edittext获得焦点后弹出软键盘,软键盘的收起实际消费了返回键事件,而之后的分发都指向了软键盘,popupwindow无法截获该事件,具体如何改我还没有想好,稍后再跟帖。

Mr_dsw
Mr_dsw   Ds   Rxr 2016.02.18 22:56

EditText获取焦点了,但是你惦记返回键是应该能关闭的

luochoudan
luochoudan   Ds   Rxr 2016.02.19 22:10

卤煮不要淘气,代码明明没问题。
public class PopwindowTest extends Activity {
Mu popupWindow;
View view;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    popupWindow = new Mu(this, 200, 100);
}

public void show(View v) {
    System.out.println("dianji");
    popupWindow.showAtLocation(v, Gravity.CENTER, 0, 0);
}

class Mu extends PopupWindow {
    Context context;

    public Mu(Context ctx, int width, int height) {
        super(width, height);
        this.context = ctx;
        view = LayoutInflater.from(context)
                .inflate(R.layout.pop_main, null);
        view.setFocusable(true);
        view.setFocusableInTouchMode(true);
        setContentView(view);
        view.setOnKeyListener(new OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                // TODO Auto-generated method stub
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    popupWindow.dismiss();
                    return true;
                }
                return false;
            }
        });
        setFocusable(true);
        setOutsideTouchable(true);
        setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
        setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

    }
}

}

activity_main.xml
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.cn.animtest.MainActivity" >

<Button
    android:id="@+id/test"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="show"
    android:text="@string/hello_world" />

<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher" />

pop_main.xml
<?xml version="1.0" encoding="utf-8"?>
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="vertical" >

<EditText
    android:id="@+id/edit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/abc_search_url_text_normal"
    android:text="haha" />

luochoudan
luochoudan   Ds   Rxr 2016.02.19 22:10

卤煮不要淘气,代码明明没问题。
public class PopwindowTest extends Activity {
Mu popupWindow;
View view;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    popupWindow = new Mu(this, 200, 100);
}

public void show(View v) {
    System.out.println("dianji");
    popupWindow.showAtLocation(v, Gravity.CENTER, 0, 0);
}

class Mu extends PopupWindow {
    Context context;

    public Mu(Context ctx, int width, int height) {
        super(width, height);
        this.context = ctx;
        view = LayoutInflater.from(context)
                .inflate(R.layout.pop_main, null);
        view.setFocusable(true);
        view.setFocusableInTouchMode(true);
        setContentView(view);
        view.setOnKeyListener(new OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                // TODO Auto-generated method stub
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    popupWindow.dismiss();
                    return true;
                }
                return false;
            }
        });
        setFocusable(true);
        setOutsideTouchable(true);
        setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
        setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

    }
}

}

activity_main.xml
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.cn.animtest.MainActivity" >

<Button
    android:id="@+id/test"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="show"
    android:text="@string/hello_world" />

<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher" />

pop_main.xml
<?xml version="1.0" encoding="utf-8"?>
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="vertical" >

<EditText
    android:id="@+id/edit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/abc_search_url_text_normal"
    android:text="haha" />

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!