2013-07-16 180 views
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; 
} 


} 

謝謝你,即使你不能幫我

回答

1

這似乎是這行代碼:

WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH 

您可以從docs瞭解更多。爲什麼你的代碼中有這條代碼?

+0

謝謝你的工作 –

+0

很高興它做到了,一定要接受答案。在未來的編碼中祝你好運。 – buczek