2016-09-20 179 views
1

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

popupWindow.setFocusable(真)

要显示键盘,使用户可以开始搜索查看打字。当searchView处于焦点状态并且软键盘处于打开状态时,则按后退键也会关闭searchView和popupWindow。

为了解决这个问题,我创建了一个自定义的搜索查看在这个推翻方法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

+0

试试这个.. popupWindow.setCancelable(false) – Meenal

+0

@MeenalSharma popupWindow没有'setCancelable'方法。 'AlertDialog'和'Dialog'确实。 –

回答

0

在您的活动中做到这一点。如果显示popupWindow,则后退按钮不会执行任何操作。

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

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

popupWindow.setOutsideTouchable(false); 

/// UPDATE

尝试改变这一点:

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

我已经按照以下方式完成了这项工作。 '弹出。setBackgroundDrawable(new BitmapDrawable()); popup.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); popup.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); popup.setOutsideTouchable(false);' 当我按下软键盘上的按钮时,活动中的OnBackPressed没有被调用,所以我创建了自定义的SearchView并检测** dispatchKeyEventPrime中的后退按钮** 但是我没有办法阻止popupWindow解雇。请帮助,如果你知道如何实现它。这将是一个很大的帮助。先进的感谢很多。 –

+0

好吧,那很好,但'Activity'的'onBackPressed()'怎么样?这就是后退键触发器 –

+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。

相关问题