弹窗popupwindow

来源:互联网 发布:u深度制作ubuntu启动盘 编辑:程序博客网 时间:2024/06/06 20:14
popupwindow是一个弹窗,也是先写一个界面,当点击button时弹出窗口(类似QQ添加表情),只是宽和高在代码中规定(不是xml布局),效果如图
···onclik···(button点击事件)
case R.id.button_dialog:
popupWindow=new PopupWindow(HellowordActivity.this);
LayoutInflater inflater1=getLayoutInflater();
View view=inflater1.inflate(R.layout.popup, null);
popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
popupWindow.setContentView(view);
popupWindow.setOutsideTouchable(true);//
外部点击消失
popupWindow.showAsDropDown(button);//放在id是button的控件下面

break;
增加点击返回键不关闭app只是关闭popupwindow,有个方法onKeyDown
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode==KeyEvent.KEYCODE_BACK){
if(popupWindow!=null&&popupWindow.isShowing()){
//&&的作用是为空后面不运行,如果不用双&就报空指针
popupWindow.dismiss();
return true;//用于使安卓机制运行if语句而不是读下面这一条
}
}

return super.onKeyDown(keyCode, event);
}

0 0