关闭

android-碎片中弹出popuwindow

标签: androidjavafragment fragmentPagpopuwindow
195人阅读 评论(0) 收藏 举报
分类:
private PopupWindow popupWindow;
private LayoutInflater mInflater;
private ViewGroup mRootView;
public void showPopWindow(View v){
if (mInflater==null||mRootView==null) {
mInflater=(LayoutInflater) getActivity().getSystemService(getActivity().LAYOUT_INFLATER_SERVICE);;
mRootView = (ViewGroup) mInflater.inflate(R.layout.date_popuwindow, null, true);//获得自定义布局
datePicker = (DatePicker) mRootView.findViewById(R.id.datePicker);
timePicker = (TimePicker) mRootView.findViewById(R.id.timePicker);
datePicker.setOnScrollListener(this);
timePicker.setOnScrollListener(this);
}
if (popupWindow==null) {
popupWindow=new PopupWindow();
popupWindow.setContentView(mRootView);
popupWindow.setWidth(300);
popupWindow.setHeight(LayoutParams.WRAP_CONTENT);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setFocusable(true);
popupWindow.setOutsideTouchable(true);
}


//解决触摸popuwindow外部dimiss掉popuwindow 内部滑动
popupWindow.setTouchInterceptor(new OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (event.getX()>0&&event.getX()<v.getWidth()) {
if (event.getY()>0&&event.getY()<v.getHeight()) {
LogUtils.i("------onTouch"+event.getX()+"-----"+event.getY()+"--"+"-----------"+v.getWidth()+"----"+v.getHeight());
return false;
}

}


popupWindow.dismiss();
leaveTimePicker();
return true;
}
});
int[] loc = new int[2];
popupWindow.showAsDropDown(v, 100, 0);

}

写一个接口

/* 接口作用:点击popupwindow窗口外部使其消失,由由主活动实现,创建popupwindow对象时回调 */

publicinterface PopupDismiss{

void setPopup(PopupWindow window); }

/*dismiss是接口对象*/dismiss = (PopupDismiss) getActivity();dismiss.setPopup(popupWindow);

/*主活动里实现这个方法*/publicvoidsetPopup(PopupWindow window){ popupWindow = window; }

/*屏幕的点击事件,重写活动的这个方法就可以了*/public boolean onTouchEvent(MotionEventevent){if(popupWindow != null && popupWindow.isShowing()){ popupWindow.dismiss(); } return super.onTouchEvent(event); }


popupWindow.setTouchInterceptor(new OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (event.getX()>0&&event.getX()<v.getWidth()) {
if (event.getY()>0&&event.getY()<v.getHeight()) {
LogUtils.i("------onTouch"+event.getX()+"-----"+event.getY()+"--"+"-----------"+v.getWidth()+"----"+v.getHeight());
return false;
}

}


popupWindow.dismiss();
leaveTimePicker();
return true;
}
});




0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:26635次
    • 积分:922
    • 等级:
    • 排名:千里之外
    • 原创:67篇
    • 转载:5篇
    • 译文:0篇
    • 评论:3条
    最新评论