Android:Overlay按钮onClick很少被调用
若我点击按钮,它会移动1,2个像素(执行ontouch()),但不会点击(onclick())。。。但是,如果我准确地单击,它就会单击,但执行onclick()函数太难了。。。。是否有任何方法可以轻松执行onclick功能。。。ontouch()功能正常Android:Overlay按钮onClick很少被调用,android,button,overlay,Android,Button,Overlay,若我点击按钮,它会移动1,2个像素(执行ontouch()),但不会点击(onclick())。。。但是,如果我准确地单击,它就会单击,但执行onclick()函数太难了。。。。是否有任何方法可以轻松执行onclick功能。。。ontouch()功能正常 WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE); overlayedButton = new Button(thi
WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
overlayedButton = new Button(this);
overlayedButton.setWidth(50);
overlayedButton.setHeight(50);
overlayedButton.setText("butn");
overlayedButton.setOnClickListener(this);
overlayedButton.setOnTouchListener(this);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(70, 70, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL|LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, PixelFormat.TRANSLUCENT);
params.gravity = Gravity.LEFT | Gravity.TOP ;
params.x = 0;
params.y = 0;
wm.addView(overlayedButton, params);
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
overlayedButton.setBackgroundResource(R.drawable.appicon_icon_hovered);
float x = event.getRawX();
float y = event.getRawY();
moving = false;
int[] location = new int[2];
overlayedButton.getLocationOnScreen(location);
originalXPos = location[0];
originalYPos = location[1];
offsetX = originalXPos - x;
offsetY = originalYPos - y;
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
int[] topLeftLocationOnScreen = new int[2];
topLeftView.getLocationOnScreen(topLeftLocationOnScreen);
System.out.println("topLeftY="+topLeftLocationOnScreen[1]);
System.out.println("originalY="+originalYPos);
float x = event.getRawX();
float y = event.getRawY();
golparams = (LayoutParams) overlayedButton.getLayoutParams();
int newX = (int) (offsetX + x);
int newY = (int) (offsetY + y);
if (Math.abs(newX - originalXPos) < 1 && Math.abs(newY - originalYPos) < 1 && !moving) {
return false;
}
golparams.x = newX - (topLeftLocationOnScreen[0]);
golparams.y = newY - (topLeftLocationOnScreen[1]);
wm.updateViewLayout(overlayedButton, golparams);
moving = true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
if (moving) {
del=new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try{
if(!Thread.currentThread().isInterrupted())
{
del.sleep(2000);
}
// if(Thread.currentThread().isInterrupted())
// {
// del.sleep(2000);
// }
}catch(InterruptedException e){}
handler.post(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
overlayedButton.setBackgroundResource(R.drawable.appicon_icon);
}
});
}
});
del.start();
return true;
}
if(moving&& originalXPos-event.getRawX()<5 ||originalYPos-event.getRawY()<5)
{
onClick(overlayedButton);
}
}
return false;
}
@Override
public void onClick(View v) {
WindowManager wm1 = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
if(v==overlayedButton)
{
// do something
}
WindowManager wm=(WindowManager)getSystemService(Context.WINDOW\u服务);
overlayedButton=新按钮(此按钮);
覆盖按钮设置宽度(50);
覆盖按钮设置高度(50);
setText(“butn”);
setOnClickListener(这个);
setOnTouchListener(这个);
WindowManager.LayoutParams params=新建WindowManager.LayoutParams(70,70,WindowManager.LayoutParams.TYPE_系统_警报,WindowManager.LayoutParams.FLAG_不可聚焦| WindowManager.LayoutParams.FLAG_不可触摸|模态| LayoutParams.FLAG_观察_触摸外,像素格式.半透明);
params.gravity=gravity.LEFT | gravity.TOP;
参数x=0;
参数y=0;
wm.addView(覆盖按钮,参数);
@凌驾
公共布尔onTouch(视图v,运动事件){
if(event.getAction()==MotionEvent.ACTION\u向下){
overlayedButton.setBackgroundResource(R.drawable.appicon_图标悬停);
float x=event.getRawX();
float y=event.getRawY();
移动=假;
int[]位置=新int[2];
覆盖按钮。getLocationOnScreen(位置);
originalXPos=位置[0];
原始ypos=位置[1];
offsetX=原始xpos-x;
offsetY=原始ypos-y;
}else if(event.getAction()==MotionEvent.ACTION\u MOVE){
int[]topLeftLocationOnScreen=新建int[2];
topLeftView.GetLocationOn屏幕(TopLeftLocationOn屏幕);
System.out.println(“topLeftY=“+topLeftLocationOnScreen[1]);
System.out.println(“originalY=“+originalYPos”);
float x=event.getRawX();
float y=event.getRawY();
golparams=(LayoutParams)overlayedButton.getLayoutParams();
int newX=(int)(抵销x+x);
int newY=(int)(offsetY+y);
if(Math.abs(newX-originalXPos)<1&&Math.abs(newY-originalYPos)<1&&moving){
返回false;
}
golparams.x=newX-(屏幕上的TopLeftLocation[0]);
golparams.y=newY-(屏幕上的TopLeftLocation[1]);
wm.UpdateView布局(覆盖按钮,golparams);
移动=真;
}else if(event.getAction()==MotionEvent.ACTION\u UP){
如果(移动){
del=新线程(新可运行(){
@凌驾
公开募捐{
//TODO自动生成的方法存根
试一试{
如果(!Thread.currentThread().isInterrupted())
{
del.sleep(2000年);
}
//如果(Thread.currentThread().isInterrupted())
// {
//del.sleep(2000年);
// }
}捕获(中断异常e){}
handler.post(新的Runnable(){
@凌驾
公开募捐{
//TODO自动生成的方法存根
overlayedButton.setBackgroundResource(R.drawable.appicon_图标);
}
});
}
});
del.start();
返回true;
}
如果(moving&&originalXPos event.getRawX()在ACTION\u DOWM.O ACTION\u UP上保存初始的X和Y,将它们与当前的X和Y进行比较。如果它们超出了您定义的阈值(50应该是好的),则手动调用onClick。我尝试了,但没有结果,您可以进一步解释或编写代码吗?