Android 安卓点击弹出窗口
我创建了一个扩展popupwindow的类。它的构造函数如下所示Android 安卓点击弹出窗口,android,onclicklistener,z-order,Android,Onclicklistener,Z Order,我创建了一个扩展popupwindow的类。它的构造函数如下所示 super(builder.context.get()); this.setWindowLayoutMode(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); setFocusable(true); setBackgroundDrawable(new BitmapDrawable()); setTouchInter
super(builder.context.get());
this.setWindowLayoutMode(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
setFocusable(true);
setBackgroundDrawable(new BitmapDrawable());
setTouchInterceptor(onTouchListener);
FrameLayout frameLayout = new FrameLayout(builder.context.get());
LayoutInflater inflater = (LayoutInflater) builder.context.get().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
View overlayBase = inflater.inflate(R.layout.tutorial_view_items, null, false);
frameLayout.addView(builder.backgroundView);
frameLayout.addView(overlayBase);
setContentView(frameLayout);
private OnTouchListener onTouchListener = new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
float x = event.getX();
float y = event.getY();
if (Math.pow(x - coordinates[0], 2) + Math.pow((y - coordinates[1]), 2) < Math.pow(
radius, 2)) {
activity.get().dispatchTouchEvent(event);
return false;
}
frameLayout.dispatchTouchEvent(event);
return true;
}
};
private OnTouchListener onTouchListener = new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
float x = event.getX();
float y = event.getY();
if (Math.pow(x - coordinates[0], 2) + Math.pow((y - coordinates[1]), 2) < Math.pow(
radius, 2)) {
activity.get().dispatchTouchEvent(event);
return false;
}
frameLayout.dispatchTouchEvent(event);
return true;
}
};
onTouchListener如下所示:
private OnTouchListener onTouchListener = new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
float x = event.getX();
float y = event.getY();
Log.d(TAG, "Received");
if (Math.pow(x - coordinates[0], 2) + Math.pow((y - coordinates[1]), 2) < Math.pow(radius, 2)) {
Log.d(TAG, "bubbled through");
return false;
}
return true;
}
};
尝试此代码可能会对您有所帮助。
或者这个链接会对你有更多帮助。。
解决方案 为弹出窗口注册触摸拦截器。如果希望活动处理它,假设您有对该活动的引用,则调用
someActivity.dispatchTouchEvent(someMotionEventPassedTotheTouchIntercept)
,如果希望popupwindow处理触控偶数,请调用someView.dispatchTouchEvent(MotionEventAssedToTouchInterceptor)
到popupwindow的内容视图,如popupwindowsetContentView(someView)
方法中设置的
我的方法具体如下
super(builder.context.get());
this.setWindowLayoutMode(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
setFocusable(true);
setBackgroundDrawable(new BitmapDrawable());
setTouchInterceptor(onTouchListener);
FrameLayout frameLayout = new FrameLayout(builder.context.get());
LayoutInflater inflater = (LayoutInflater) builder.context.get().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
View overlayBase = inflater.inflate(R.layout.tutorial_view_items, null, false);
frameLayout.addView(builder.backgroundView);
frameLayout.addView(overlayBase);
setContentView(frameLayout);
private OnTouchListener onTouchListener = new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
float x = event.getX();
float y = event.getY();
if (Math.pow(x - coordinates[0], 2) + Math.pow((y - coordinates[1]), 2) < Math.pow(
radius, 2)) {
activity.get().dispatchTouchEvent(event);
return false;
}
frameLayout.dispatchTouchEvent(event);
return true;
}
};
private OnTouchListener onTouchListener = new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
float x = event.getX();
float y = event.getY();
if (Math.pow(x - coordinates[0], 2) + Math.pow((y - coordinates[1]), 2) < Math.pow(
radius, 2)) {
activity.get().dispatchTouchEvent(event);
return false;
}
frameLayout.dispatchTouchEvent(event);
return true;
}
};
private-OnTouchListener-OnTouchListener=new-OnTouchListener(){
@凌驾
公共布尔onTouch(视图v,运动事件){
float x=event.getX();
float y=event.getY();
if(Math.pow(x坐标[0],2)+Math.pow((y坐标[1]),2)
您能澄清一下“冒泡”是否真的被记录在您希望通过弹出窗口的点击上吗?是的,很抱歉造成混淆。正在调用ontouchlistener,然后根据我触摸的位置(即,当我触摸圆圈区域时“冒泡通过”)相应地跟踪日志输出。如果需要黑客解决方案,可以检查触摸是否在按钮的xy坐标内,并执行performClick()。或者可以将触摸显式传递到包含按钮的活动/视图的onTouch。希望有人能给你一个更好的答案,这是不幸的。您认为通过不同的视图容器来实现这个想法会更好吗?对话对话片段?有没有在视图中点击锚定活动的经验?我已经用解决方案更新了我的问题。嗯,谢谢?我对给弹出窗口充气没有问题。问题是我无法将触摸事件转发到popupwindow定位到的视图。就我所见,这个实现不在您提供的代码段中…有必要粗鲁一点吗?“嗯,谢谢?”是不合适的,因为Hitesh在解释你的问题时试图提供帮助。喂MotionEventPassedToTouchInterceptor
您可以告诉如何执行此操作的更多详细信息?请查看传递给onTouchListener
的参数之一的类型如何为MotionEvent
?如果你有一个对底层活动的引用,那么通过调用你的activity.dispatchTouchEvent(motionEvent)(然后rrturn false!)来传递该touch事件。这就在这里,给了我一个被卡住3天后的解决方案。你就是那个人!