Android PopupWindow 点击外面取消实现代码
程序员文章站
2022-07-03 18:47:07
private void showpopupview()
{
if (mpopupwindow == null)
{
vie...
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);//需要设置为ture,表示可以聚焦 //需要设置背景,用物理键返回的时候 mpopupwindow.setbackgrounddrawable(new bitmapdrawable(getresources())); mpopupwindow.setoutsidetouchable(true); view.setontouchlistener(new ontouchlistener()// 需要设置,点击之后取消popupview,即使点击外面,也可以捕获事件 { 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);// 显示再指定控件的下面 } }