android-碎片中弹出popuwindow
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;
}
});




阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/joshua_love/article/details/52372831
个人分类: android应用编程
想对作者说点什么? 我来说一句

recyclerview 适配器封装

2017年08月04日 20.95MB 下载

没有更多推荐了,返回首页

不良信息举报

android-碎片中弹出popuwindow

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭