如何在android中的按钮内获取滚动鼠标事件

时间:2016-04-06 06:13:30

标签: java android eclipse motionevent

单击按钮后,鼠标滚动将被激活并向上滚动,按钮值应该增加,当向下滚动时值减少。但按钮外部代码工作,然后按钮内部代码不工作。我的示例代码在这里。

public class MainActivity extends Activity {

Button button;
int x,f;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button=(Button)findViewById(R.id.button1);


    button.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View arg0, MotionEvent arg1) {
            // TODO Auto-generated method stub
            // i need implement code here ,how can i do?                
            return false;
        }
    });
}
@SuppressLint("NewApi") @Override
public boolean onGenericMotionEvent(MotionEvent event) {
    //if (0 != (event.getSource() & InputDevice.SOURCE_CLASS_POINTER)) {
    switch (event.getAction()) {
    case MotionEvent.ACTION_SCROLL:
        if (event.getAxisValue(MotionEvent.AXIS_VSCROLL) < 0.0f)
            //selectNext();
        {
            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 true;
        }
    }
    return super.onGenericMotionEvent(event);
}   }
  

我需要触摸听众内部的这些功能我不知道该怎么办,请帮助我!

0 个答案:

没有答案