2015-07-28 329 views
1

我沒有能夠設置onGenericMotionEvent上的按鈕或任何觀點。意思是我沒有從onGenericMotionEvent方法得到任何迴應。onGenericMotionEvent Android上的按鈕

下面是我的代碼:

<Button 
       android:id="@+id/btnMouse" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" /> 

我的Java文件編碼:

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_remote_controls); 
btn1 = (Button) findViewById(R.id.btnMouse); 
} 

@Override 
public boolean onGenericMotionEvent(MotionEvent event) { 
    int pointerCount = event.getPointerCount(); 

    switch (event.getAction() & event.ACTION_MASK) { 
     case MotionEvent.ACTION_POINTER_DOWN: 
      Log.e("Mouse: ", "Right Click"); 
      return true; 
     case MotionEvent.ACTION_DOWN: 
      Log.w("Mouse: ", "Left Click"); 
      return true; 
     case MotionEvent.ACTION_HOVER_MOVE: 
      if (pointerCount == 1) { 
       Log.e("Mouse: ", "Move"); 
      } else if (pointerCount == 2) { 
       Log.e("Mouse: ", "Scroll"); 
      } 
      return true; 
    } 
    return false; 
} 

,當我在按鈕使用setOnTouchListener它的作品完美,但我不能讓我的按鈕MotionEvent.ACTION_HOVER_MOVE,當我閱讀文檔它指定setOnTouchListener不能處理MotionEvent.ACTION_HOVER_MOVE所以這就是爲什麼我要使用的onGenericMotionEvent我的按鈕獲得MotionEvent.ACTION_HOVER_MOVE行動。

或任何其他方式來實現按鈕MotionEvent.ACTION_HOVER_MOVE

但我不知道如何能做到這一點,請大家幫忙

EDITED

由Android按鈕,我想模擬真實的鼠標,意味着當用戶拖到它比我想移動我的筆記本鼠標。

當我使用MotionEvent.ACTION_MOVE:它的工作原理,它移動我的筆記本電腦鼠標,但我的鼠標之前移動它改變筆記本電腦的鼠標指針的位置,它是點擊按鈕,因此忽略了Android的提供ACTION_HOVER_MOVE它不執行ACTION_DOWN不像ACTION_MOVE

+0

你需要什麼ACTION_HOVER_MOVE? – pskink

+0

@pskink,我想模擬真正的鼠標,當用戶移動手指在按鈕上時,我想移動筆記本電腦鼠標的手段。 –

+0

@pskink,總之我想處理我的android按鈕中的所有鼠標事件。 –

回答

0

嘿,我得到了你的問題,你想有一個懸停效果,當你觸摸它的按鈕來工作。

您可以使用下面的代碼只是這種方法通過您的按鈕的實例,還可以自定義顏色懸停效果:

public void buttonEffect(View button){ 
     button.setOnTouchListener(new OnTouchListener() { 
      public boolean onTouch(View v, MotionEvent event) { 
       switch (event.getAction()) { 
       case MotionEvent.ACTION_DOWN: { 
        v.getBackground().setColorFilter(Color.parseColor("#00BFFF"),PorterDuff.Mode.SRC_ATOP); 
        v.invalidate(); 
        break; 
       } 
       case MotionEvent.ACTION_UP: { 
        v.getBackground().clearColorFilter(); 
        v.invalidate(); 
        break; 
       } 

       case MotionEvent.ACTION_CANCEL:{ 
        v.getBackground().clearColorFilter(); 
        v.invalidate(); 
        break; 
       } 
       } 
       return false; 
      } 
     }); 
    } 

希望這有助於你...!

+1

我不想對我的按鈕產生任何影響,我只想處理按鈕上的'MotionEvent.ACTION_HOVER_MOVE',請你幫我做一下嗎? –