【问题标题】:Keep showing popupWindow when back button of keyboard is pressed按下键盘的后退按钮时继续显示popupWindow
【发布时间】:2016-09-20 13:14:22
【问题描述】:

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

popupWindow.setFocusable(true)

显示键盘以便用户可以开始在 searchView 中输入。当 searchView 处于焦点且软键盘打开时,按返回键同时关闭 searchView 和 popupWindow。

为了解决这个问题,我创建了一个 custom 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 关闭。

【问题讨论】:

  • 试试这个.. popupWindow.setCancelable(false)
  • @MeenalSharma popupWindow 没有setCancelable 方法。 AlertDialogDialog 可以。

标签: android popupwindow


【解决方案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);
    }
});

确保在你的 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

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() 会被调用。

如您所见,我在按下后退按钮时添加了一个Toast,并且每次都会调用它。试试看。它不会关闭 PopupWindow。

【讨论】:

    【解决方案2】:

    在您的活动中执行此操作。如果 popupWindow 正在显示,则返回按钮不会做任何事情。

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

    您也可以使用此功能,这样当在弹出窗口之外单击时,它不会关闭。

    popupWindow.setOutsideTouchable(false);
    

    /// 更新

    尝试改变这个:

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

    【讨论】:

    • 我已经通过以下方式完成了这项工作。 popup.setBackgroundDrawable(new BitmapDrawable()); popup.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); popup.setOutsideTouchable(false); OnBackPressed 在我按下软键盘上的后退按钮时没有被调用,所以我创建了自定义 SearchView 并在 dispatchKeyEventPrime 中检测到后退按钮但我没有得到任何方法我可以停止 popupWindow 解雇。请帮助,如果你知道如何实现它。这将是一个很大的帮助。非常感谢。
    • 好的,但是ActivityonBackPressed() 呢?这就是返回键触发的原因
    • 当软键盘打开时按下返回按钮时,Activity 的 Backpress 不会被调用。
    • 看看我的编辑。更改dispatchKeyEventPreIme。我犯了一个错误。又做了一次改动
    • 好的,我正在检查这个解决方案并会更新你。
    猜你喜欢
    • 1970-01-01
    • 2013-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-28
    • 1970-01-01
    相关资源
    最近更新 更多