2010-06-29 26 views
9

Ngay bây giờ tất cả những gì tôi đang cố gắng làm là phát hiện khi màn hình được nhấn và sau đó hiển thị thông báo tường trình để xác nhận điều đó đã xảy ra. Mã của tôi cho đến nay được sửa đổi tắt của mã mẫu CameraPreview (nó cuối cùng sẽ chụp ảnh) để phần lớn mã nằm trong một lớp mở rộng SurfaceView. API cho mã ví dụ từ SDK là 7.Làm thế nào để phát hiện đầu vào cảm ứng trên Android

Trả lời

19

Thử mã bên dưới để phát hiện sự kiện chạm.

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

Để hiển thị sử dụng hộp thoại Phương thức hoạt động showDialog(int). Bạn phải thực hiện onCreateDialog(). Xem tài liệu để biết chi tiết.

4

tôi đã làm nó như thế này:

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 
    } 
} 

trong xml của tầm nhìn của bạn, ghi rõ:

<?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

Dưới đây là một ví dụ đơn giản về cách để phát hiện một cách đơn giản về sự kiện liên lạc, nhận coords và hiển thị một bánh mì nướng. Sự kiện trong exmple này là hành động xuống, di chuyển và hành động lên.

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; 
    } 
} 
Các vấn đề liên quan