簡體   English   中英

按鈕單擊以使用鼠標滾動工作設置OnGenericMotionListener 事件

[英]Button click to setOnGenericMotionListener event using mouse scroll work

先生,我正在使用按鈕鍵,如果我將鼠標光標點移動到按鈕,它會顯示一些類似於 Eg:5 的值,然后在 setOnGenericMotionListener 事件上使用鼠標滾動 Eg: 5 會根據鼠標的不同而增加和減少值滾動但現在我想移動鼠標光標點的任何位置需要使用鼠標滾動事件設置 setOnGenericMotionListener 事件來處理該特定按鈕如何執行此事件?

活動

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

您不應使用此方法來檢測觸摸事件。 在 View.onGenericmotionEvent 描述中:

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

您可以在此處找到此方法的一些使用案例

順便說一句,您應該改用GestureDetector來處理您的手勢。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM