本文介绍了按下键盘的后退按钮时继续显示popupWindow的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am working on an Android application and I've designed a popupWindow to show some view by replacing a soft keyboard in an activity. The popupWindow contains a searchView. When I click on a button, it shows the popupWindow and the soft keyboard gets hidden. Now when the searchView in the popupWindow gets focus, I call the method:

显示键盘,以便用户可以开始在searchView中键入.当searchView处于焦点状态并且软键盘处于打开状态时,然后按返回键也将同时关闭searchView和popupWindow.

To show keyboard so that users can start typing in searchView. When the searchView is in focus and the soft keyboard is open, then pressing the back key closes both the searchView and the popupWindow as well.

为解决此问题,我创建了一个自定义searchView ,并在此方法中覆盖了 dispatchKeyEventPreIme 方法.

To solve this issue, I created a custom searchView and overrode the method dispatchKeyEventPreIme in this as below.

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关闭.

I have cleared the focus from the searchView when the back button is pressed, but I don't know how can I prevent the popupWindow from closing.

推荐答案

这就是我尝试过的并且有效的方法!

Here's what I tried and it worked!

在onCreate()中:

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

请确保您的活动中包含此内容:

Make sure to have this in your Activity:

@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

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不会被调用,但是Activity的OnBackPressed()会被调用.

When I do this implementation, CustomSearchView's dispatchKeyEventPreIme does not called BUT Activity's OnBackPressed() does get called.

如您所见,当按下后退按钮时,我会放置一个Toast,并且每次都会调用.试试看.它不会关闭PopupWindow.

As you see, I put a Toast when back button gets pressed and it calls each time. Try it out. It doesn't close the PopupWindow.

这篇关于按下键盘的后退按钮时继续显示popupWindow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 22:12