即使我没有点击按钮,也会触摸侦听器

时间:2013-07-16 18:55:37

标签: java android

好吧,我有一个来自另一个问题的示例代码,是一个“永远在顶部的按钮应用程序”,并在屏幕上的每次触摸时响应。 任何人都可以帮我解决这个问题吗? 我希望应用程序仅在我按下按钮时才会响应.. 这是代码:

public class HUD extends Service implements OnClickListener, OnTouchListener {
Button mButton;
@Override
public IBinder
onBind(Intent intent) {
return null;
}

@Override
public void onCreate() {
    super.onCreate();
    //mView = new HUDView(this);

    mButton = new Button(this);
    mButton.setText("Screenshot");
    mButton.setClickable(true);
    mButton.setOnTouchListener(this);


    WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_PHONE, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                    | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT);
    params.gravity = Gravity.LEFT | Gravity.TOP;
    params.setTitle("Load Average");
    WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
    wm.addView(mButton, params);
}

@Override
public void onDestroy() {
    super.onDestroy();
    if(mButton != null)
    {
        ((WindowManager) getSystemService(WINDOW_SERVICE)).removeView(mButton);
        mButton = null;
    }
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    Toast.makeText(this,"Overlay button event", Toast.LENGTH_SHORT).show(); //where it seems to be the problem
    return false;
}


}

谢谢你,即使你无法帮助我

1 个答案:

答案 0 :(得分:1)

似乎是这行代码:

WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH

您可以从docs了解有关此内容的更多信息。为什么你的代码中有这一行?