How to build a SingleTouchEven in android

1.Code:

View view = findViewById(R.id.activity_main);
view.setOnTouchListener(new View.OnTouchListener(){
String startX,endX;
@Override
public boolean onTouch(View view, MotionEvent event) {
float eventX = event.getX();
boolean re=false;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
startX= String.valueOf(eventX);
re = true;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
// nothing to do
float xstart=Float.parseFloat(startX);
if(xstart< eventX){
if(currentImage==0){
currentImage= images.length -1;
}else {
currentImage–;
}
ImageView preWallpaper = (ImageView) findViewById(R.id.wallpaper);
preWallpaper.setImageResource(images[currentImage]);
Toast.makeText(getApplicationContext(), “Pre Successes”, Toast.LENGTH_SHORT).show();
}else{
if(currentImage==images.length-1){
currentImage= 0;
}else {
currentImage++;
}
ImageView preWallpaper = (ImageView) findViewById(R.id.wallpaper);
preWallpaper.setImageResource(images[currentImage]);
Toast.makeText(getApplicationContext(), “Next Successes”, Toast.LENGTH_SHORT).show();
}
break;
default:
re = false;
}
return re;
}
});

2. Link tham khảo:
http://www.vogella.com/tutorials/AndroidTouch/article.html

Advertisements