0

好吧,我有一个来自另一个问题的示例代码,它是一个“始终在顶部按钮应用程序”,并且在屏幕上的每一次触摸时都会做出响应。谁能帮我解决这个问题?我希望应用程序仅在我按下按钮时响应。这是代码:

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;
}


}

谢谢,即使你帮不了我

4

1 回答 1

1

它似乎是这行代码:

WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH

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

于 2013-07-16T19:11:44.990 回答