2010-05-30 44 views
23

Tôi mới sử dụng Android, tôi đã theo dõi hướng dẫn thế giới xin chào và có ý tưởng cơ bản về những gì đang diễn ra. Tôi đặc biệt quan tâm đến màn hình cảm ứng của T-Mobile Pulse vì vậy chỉ để tôi bắt đầu tôi muốn có thể viết các tọa độ của một sự kiện tocuh trên màn hình, vì vậy người dùng đã chạm vào phối hợp 5 , 2 - một lần xem văn bản trên màn hình sẽ hiển thị điều đó.Nhận tọa độ của sự kiện chạm trên Android

Hiện nay tôi có một chương trình đơn giản rằng chỉ cần tải một file xml chứa TextView tôi có ý định viết tọa độ trong.

Cảm ơn bạn trước, tôi đã làm Google để được giúp đỡ và tìm kiếm stackoverflow nhưng tất cả mọi thứ Tôi tìm thấy hoặc là đã đi qua đầu của tôi hoặc không thích hợp cho việc này. Chúc mừng.

Trả lời

42

Bạn có thể sử dụng chức năng này: http://developer.android.com/reference/android/view/View.html#setOnTouchListener(android.view.View.OnTouchListener)

Bạn có thể sẽ đặt nó trong phương thức onCreate của bạn khoảng cách này (thử nghiệm thời gian này): Mã

Hoạt động onCreate

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    final TextView textView = (TextView)findViewById(R.id.textView); 
    // this is the view on which you will listen for touch events 
    final View touchView = findViewById(R.id.touchView); 
    touchView.setOnTouchListener(new View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      textView.setText("Touch coordinates : " + 
       String.valueOf(event.getX()) + "x" + String.valueOf(event.getY())); 
       return true; 
     } 
    }); 
} 

mã bố trí

<?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" 
    > 
<TextView 
    android:id="@+id/touchView" 
    android:background="#f00" 
    android:layout_weight="1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    /> 
<TextView 
    android:id="@+id/textView" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Hello World, TestActivity" 
    /> 
</LinearLayout> 

Chỉnh sửa: Tôi đã thử mã của mình và thực sự có một vài lỗi. Dù sao, với bố trí tôi cung cấp cho bạn ở đây nó hoạt động trên giả lập của tôi. Bạn có thể cung cấp thêm mã/ngữ cảnh để tôi có thể thấy điều gì sai?

+0

Bạn có thể muốn nhìn vào http://stackoverflow.com/questions/2068007/android-how -do-i-get-raw-touch-screen-thông tin cho giải pháp khác. –

+0

Tôi đã thử mã của bạn và thực hiện một số chỉnh sửa mà tôi có thể nhưng tôi thật không may là nó không thể hoạt động được. Cảm ơn bạn đã dành thời gian để giúp tôi mặc dù tôi không thể làm cho nó hoạt động (đó là lỗi của tôi vì không thực sự biết Java) – Joe

+0

Cảm ơn bạn rất nhiều :) Điều đó làm việc một điều trị – Joe

3

Có vẻ như bạn muốn nhận các sự kiện liên lạc từ toàn bộ khu vực bố cục của mình cho thử nghiệm cụ thể này. Thử gắn trình xử lý cảm ứng vào chế độ xem cha mẹ của bạn chứ không phải chế độ xem mục tiêu riêng.

Đây không phải là cách tiếp cận rất phổ biến. Trong hầu hết các trường hợp, bạn sẽ muốn nghe các sự kiện cảm ứng ở chế độ xem con cụ thể và nếu bạn có các chế độ xem khác trong bố cục xử lý các sự kiện chạm (chẳng hạn như nút), chúng sẽ được ưu tiên hơn chế độ xem gốc. Xem http://developer.android.com/guide/topics/ui/ui-events.html để biết thêm thông tin về cách xử lý sự kiện giao diện người dùng.

Layout (touch_viewer.xml):

<?xml version="1.0" encoding="utf-8" ?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:id="@+id/parent" > 
    <TextView android:id="@+id/text" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Hello, World!" /> 
</LinearLayout> 

Và trong hoạt động của bạn:

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

    final LinearLayout parent = (LinearLayout) findViewById(R.id.parent); 
    final TextView text = (TextView) findViewById(R.id.text); 
    parent.setOnTouchListener(new OnTouchListener() { 
     public boolean onTouch(View v, MotionEvent ev) { 
      text.setText("Touch at " + ev.getX() + ", " + ev.getY()); 
      return true; 
     } 
    }); 
} 
+0

gì nhập khẩu Tôi cần thêm, vì tôi chỉ nhận được lỗi :( Mô tả \t Resource \t Đường dẫn \t Location \t Loại MotionEvent không thể giải quyết cho một loại \t AndroidTablet.java \t/AndroidTablet/src/joehollo/androidtablet \t dòng 22 \t Java vấn đề R.layout.touch_viewer không thể giải quyết \t AndroidTablet.java \t/androidTablet/src/joehollo/androidtablet \t dòng 17 \t Java vấn đề loại mới View.OnTouchListener() {} phải thực hiện kế thừa trừu tượng phương pháp Xem .OnTouchListener.onTouch (Xem, MotionEv ent) \t AndroidTablet.java \t/AndroidTablet/src/joehollo/androidtablet \t dòng 21 \t Vấn đề Java – Joe

+0

+1 công trình này rất tốt cho tôi, cảm ơn –

0

Các công trình sau đây sử dụng một màn hình cảm ứng trên ứng dụng android mẫu jetboy. Mã Joes hoạt động với một tinh chỉnh để làm nổi bật nền. Dòng duy nhất tôi đã thêm là

android:background="#0000" 
android:layout_height="fill_parent" 
android:layout_height="fill_parent" 

Cảm ơn Joe (ở trên).

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    final TextView textView = (TextView)findViewById(R.id.textView); 
    // this is the view on which you will listen for touch events 
    final View touchView = findViewById(R.id.touchView); 
    touchView.setOnTouchListener(new View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      textView.setText("Touch coordinates : " + 
       String.valueOf(event.getX()) + "x" + String.valueOf(event.getY())); 
       return true; 
     } 
    }); 
} 

mã bố trí

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <com.android.samples.jetboy.JetBoyView android:id="@+id/JetBoyView" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" /> 
     <TextView 
       android:id="@+id/touchView" 
       android:background="#0000" 
       android:layout_weight="1" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
     /> 
     <TextView 
       android:id="@+id/textView" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:text="Hello World, TestActivity" 
     /> 
</FrameLayout> 
0

Phiên bản chỉnh sửa mà không có sự sai sót:

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.view.MotionEvent; 
import android.view.View; 
import android.widget.TextView; 
public class MainActivity extends Activity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     final TextView textView = (TextView) findViewById(R.id.textView); 
     // this is the view on which you will listen for touch events 
     final View touchView = findViewById(R.id.touchView); 
     touchView.setOnTouchListener(new View.OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       textView.setText("Touch coordinates : " 
         + String.valueOf(event.getX()) + "x" 
         + String.valueOf(event.getY())); 
       return true; 
      } 
     }); 
    } 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 
} 
Các vấn đề liên quan