2016-04-28 24 views
1

Sir, Ich arbeite mit auf Taste Taste und es zeigen einige Wert wie für zB: 5 wenn ich Mauszeiger auf Schaltfläche bewegen, dann auf SetOnGenericMotionListener Ereignis mit der Maus blättern die ZB: 5 wird variiert, um den Wert zu erhöhen und zu verringern, hängt vom Maus-Scroll ab, aber jetzt möchte ich jeden Ort meines Mauscursorpunktes bewegen, um das setOnGenericMotionListener-Ereignis mit dem Maus-Scroll-Ereignis zu setzen, um an dieser speziellen Schaltfläche zu arbeiten ?Button klicken, um setOnGenericMotionListener Ereignis mit Maus Scroll-Arbeit

Aktivität

public class MainActivity extends Activity { 
Button button; 
int x,f; 
@SuppressLint("NewApi") @Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    button=(Button)findViewById(R.id.button1);     
    button.setOnGenericMotionListener(new OnGenericMotionListener() { 
    @Override 
     public boolean onGenericMotion(View v, MotionEvent event) { 
      // TODO Auto-generated method stub 
      switch (event.getAction()) { 
      case MotionEvent.ACTION_SCROLL:    
       if (event.getAxisValue(MotionEvent.AXIS_VSCROLL) > 0.0f) 
       { 
        x=Integer.parseInt(button.getText().toString()); 
        f=x+5; 
        button.setText(""+f); 
       } 
       else 
       { 
        x=Integer.parseInt(button.getText().toString()); 
        f=x-5; 
        button.setText(""+f); 
       } 
      } 
      return false; 
     } 
    }); 
}} 

Antwort

1

Sie diese Methode nicht das Berührungsereignis zu erfassen, verwenden sollten. In View.onGenericmotionEvent Beschreibung:

/** 
    * Implement this method to handle generic motion events. 
    * <p> 
    * Generic motion events describe joystick movements, mouse hovers, track pad 
    * touches, scroll wheel movements and other input events. The 
    * {@link MotionEvent#getSource() source} of the motion event specifies 
    * the class of input that was received. Implementations of this method 
    * must examine the bits in the source before processing the event. 
    * The following code example shows how this is done. 
    * </p><p> 
    * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER} 
    * are delivered to the view under the pointer. All other generic motion events are 
    * delivered to the focused view. 
    * </p> 
    * <pre> public boolean onGenericMotionEvent(MotionEvent event) { 
    *  if (event.isFromSource(InputDevice.SOURCE_CLASS_JOYSTICK)) { 
    *   if (event.getAction() == MotionEvent.ACTION_MOVE) { 
    *    // process the joystick movement... 
    *    return true; 
    *   } 
    *  } 
    *  if (event.isFromSource(InputDevice.SOURCE_CLASS_POINTER)) { 
    *   switch (event.getAction()) { 
    *    case MotionEvent.ACTION_HOVER_MOVE: 
    *     // process the mouse hover movement... 
    *     return true; 
    *    case MotionEvent.ACTION_SCROLL: 
    *     // process the scroll wheel movement... 
    *     return true; 
    *   } 
    *  } 
    *  return super.onGenericMotionEvent(event); 
    * }</pre> 
    * 
    * @param event The generic motion event being processed. 
    * @return True if the event was handled, false otherwise. 
    */ 
    public boolean onGenericMotionEvent(MotionEvent event) { 
     return false; 
    } 

für diese Methode Einige Fälle der Nutzung können Sie here

Btw finden Sie GestureDetector stattdessen Ihre Geste zu behandeln verwenden sollten.