Está en la página 1de 1

public class MainActivity extends ActionBarActivity implements OnTouchListener{

StringBuilder txt = new StringBuilder();


TextView txtPosicion;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtPosicion = (TextView) findViewById(R.id.tv_Posicion);
txtPosicion.setText("Toca aqu y arrastra con el dedo");
// Fijo el listener en el textView
txtPosicion.setOnTouchListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
txt.setLength(0); // Limpio el string
// Identifico el evento ocurrido
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
txt.append("ACTION_DOWN: ");
break;
case MotionEvent.ACTION_MOVE:
txt.append("ACTION_MOVE: ");
break;
case MotionEvent.ACTION_UP:
txt.append("ACTION_UP: ");
break;
case MotionEvent.ACTION_CANCEL:
txt.append("ACTION_CANCEL: ");
break;
}
// Construyo un texto para mostrar la coordenada
txt.append(event.getX());
txt.append(", ");
txt.append(event.getY());
// Muestro el texto
txtPosicion.setText(txt.toString());
return true;
}
}

También podría gustarte