简体   繁体   中英

when button click to activated mouse scroll in android

After button click to mouse scroll to be activted,then scroll up to value increment and scroll down to value decrement.Button inside code here not working, my sample code here,please analysis code to help me.

enter code here
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() {           
        @SuppressLint("NewApi") @Override
        public boolean onTouch(View arg0, MotionEvent event) {
            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 false;
        }           
    });

}

You can implement a ScrollView as your layout in your UI.

With this layout you can program your button to handle the click and use the ScrollView.scrollBy(x,y) function to do the necessary.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM