Android PopupWindow click outside to cancel the implementation code
- 2020-06-19 11:41:21
- OfStack
private void showPopupView()
{
if (mPopupWindow == null)
{
View view = getLayoutInflater().inflate(R.layout.newest_layout, null);
mPopupWindow = new PopupWindow(view, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
mPopupWindow.setFocusable(true);// I need to set it to ture , means you can focus
// You need to set the background and return it with the physical key
mPopupWindow.setBackgroundDrawable(new BitmapDrawable(getResources()));
mPopupWindow.setOutsideTouchable(true);
view.setOnTouchListener(new OnTouchListener()// You need to set it, click and cancel popupview , you can capture the event even by clicking on the outside
{
public boolean onTouch(View v, MotionEvent event)
{
if (mPopupWindow.isShowing())
{
Trace.Log("-------------------onTouch------------");
mPopupWindow.dismiss();
}
return false;
}
});
}
if (mPopupWindow.isShowing())
{
mPopupWindow.dismiss();
}
else
{
View parent = findViewById(R.id.newest);
mPopupWindow.showAsDropDown(parent);// Displays the bottom of the specified control
}
}