2010-06-29 8 views
9

En este momento, lo único que intento hacer es detectar cuando se presiona la pantalla y luego mostrar un mensaje de registro para confirmar que sucedió. Mi código hasta el momento se ha modificado fuera del código de muestra de CameraPreview (eventualmente tomará una foto) por lo que la mayor parte del código está en una clase que extiende SurfaceView. API para el código de ejemplo del SDK es 7.Cómo detecto la entrada táctil en Android

Respuesta

19

Pruebe el siguiente código para detectar eventos táctiles.

mView.setOnTouchListener(new OnTouchListener() { 
    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     //show dialog here 
     return false; 
    } 
}); 

Para mostrar el diálogo utilice el método de actividad showDialog(int). Debe implementar onCreateDialog(). Ver la documentación para más detalles.

4

lo hice así:

public class ActivityWhatever extends Activity implements OnTouchListener 
{ 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.yourlayout); 

     //the whole screen becomes sensitive to touch 
     mLinearLayoutMain = (LinearLayout) findViewById(R.id.layout_main); 
     mLinearLayoutMain.setOnTouchListener(this); 
    } 

    public boolean onTouch(View v, MotionEvent event) 
    { 
     // TODO put code in here 

     return false;//false indicates the event is not consumed 
    } 
} 

en el xml de la vista, especifique:

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/layout_main"> 

    <!-- other widgets go here--> 

</LinearLayout> 
13

Aquí es un ejemplo sencillo de cómo detectar un sencillo en el evento táctil, obtener coordenadas y muestra un brindis El evento en este ejemplo es Acción hacia abajo, Mover y Acción hacia arriba.

import android.app.Activity; 
import android.os.Bundle; 
import android.view.MotionEvent; 
import android.widget.Toast; 

public class MainActivity extends Activity { 

    private boolean isTouch = false; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 

     int X = (int) event.getX(); 
     int Y = (int) event.getY(); 
     int eventaction = event.getAction(); 

     switch (eventaction) { 
      case MotionEvent.ACTION_DOWN: 
       Toast.makeText(this, "ACTION_DOWN AT COORDS "+"X: "+X+" Y: "+Y, Toast.LENGTH_SHORT).show(); 
       isTouch = true; 
       break; 

      case MotionEvent.ACTION_MOVE: 
       Toast.makeText(this, "MOVE "+"X: "+X+" Y: "+Y, Toast.LENGTH_SHORT).show(); 
       break; 

      case MotionEvent.ACTION_UP: 
       Toast.makeText(this, "ACTION_UP "+"X: "+X+" Y: "+Y, Toast.LENGTH_SHORT).show(); 
       break; 
     } 
     return true; 
    } 
} 
Cuestiones relacionadas