按键盘后退按钮时,继续显示popupWindow

时间:2016-09-20 13:14:22

标签: android popupwindow

我正在开发一个Android应用程序,我设计了一个popupWindow来通过替换活动中的软键盘来显示一些视图。 popupWindow包含一个searchView。当我点击一个按钮时,它会显示popupWindow并且软键盘会被隐藏。现在当popupWindow中的searchView获得焦点时,我调用方法:

  

popupWindow.setFocusable(真)

显示键盘,以便用户可以开始在searchView中输入内容。当searchView处于焦点并且软键盘打开时,按后退键也会关闭searchView和popupWindow。

要解决此问题,我创建了一个自定义searchView 并覆盖了 dispatchKeyEventPreIme 方法,如下所示。

public class CustomSearchView extends SearchView {

    Context context;
    AppConstant mAppConst;

    public CustomSearchView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }


    @Override
    public boolean dispatchKeyEventPreIme(KeyEvent event) {

       if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
           clearFocus();
        }
        return super.dispatchKeyEventPreIme(event);
    }
}

按下后退按钮时,我已从searchView清除焦点,但我不知道如何阻止popupWindow关闭。

enter image description here

2 个答案:

答案 0 :(得分:1)

这是我尝试过的,它有效!

在onCreate()中:

private PopupWindow popupWindow;

someButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        final LayoutInflater inflater = (LayoutInflater) appContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View popupView = inflater.inflate(R.layout.popupwindow_view, null, false);

        popupWindow = new PopupWindow(popupView, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        popupWindow.setOutsideTouchable(false);

        CustomSearchView searchView = (CustomSearchView) popupView.findViewById(R.id.customSearchView);
        popupWindow.showAsDropDown(someButton, 50, -30);
    }
});

确保在您的活动中有这个:

@Override
public void onBackPressed() {
    if (popupWindow != null && popupWindow.isShowing()) {
        Toast.makeText(context, "Activity Back Pressed - PopupWindow showing!", Toast.LENGTH_SHORT).show();
    } else {
        super.onBackPressed();
}

我的XML布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.package.name.CustomSearchView
        android:layout_width="match_parent"
        android:id="@+id/customSearchView"
        android:background="@android:color/black"
        android:layout_height="match_parent"></com.package.name.CustomSearchView>

</LinearLayout>

CustomSearchView.java

public class CustomSearchView extends SearchView {

    Context context;

    public CustomSearchView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    @Override
    public boolean dispatchKeyEventPreIme(KeyEvent event) {
       if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
           clearFocus();
           Toast.makeText(context, "CustomSearchView Back Pressed", Toast.LENGTH_SHORT).show();
           return false;
       } else {
           return super.dispatchKeyEventPreIme(event);
       }
    }
}

当我执行此实现时,CustomSearchView的dispatchKeyEventPreIme未调用BUT Activity OnBackPressed()会被调用。

如你所见,当按下后退按钮时我按下Toast并且每次调用它。试试看。它不会关闭PopupWindow。

答案 1 :(得分:0)

在您的活动中执行此操作。如果popupWindow显示,后退按钮将不会执行任何操作。

@Override
public void onBackPressed() {
    if (popupWindow != null && popupWindow.isShowing()) {
        // do nothing
    } else {
        super.onBackPressed();
    }
}

您也可以使用此功能,因此当在popupWindow外部点击时,它不会关闭。

popupWindow.setOutsideTouchable(false);

///更新

尝试更改此内容:

@Override
public boolean dispatchKeyEventPreIme(KeyEvent event) {
    if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
       clearFocus();
       return false;
    } else {
       return super.dispatchKeyEventPreIme(event);
    }
}