2010-05-30 122 views
23

我是Android新手,我跟隨了hello world tutorial並對發生了什麼有一個基本的想法。我對我的T-Mobile Pulse的觸摸屏特別感興趣,所以爲了讓我開始,我希望能夠在屏幕上編寫一個tocuh事件的座標,所以說用戶觸摸了座標5 ,2 - 屏幕上的文本視圖會顯示。獲取Android上的觸摸事件的座標

目前我有一個只是用來加載包含我打算寫的座標在TextView的一個XML文件的簡單程序。

預先感謝您,我做了谷歌的幫助和搜索計算器,但一切我發現要麼走過我的頭,要麼不適合這個。乾杯。

回答

42

您可以使用此功能:http://developer.android.com/reference/android/view/View.html#setOnTouchListener(android.view.View.OnTouchListener)

你可能把它放在你的大致onCreate方法這種方式(這次測試):

活動的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; 
     } 
    }); 
} 

佈局代碼

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

編輯:我嘗試了我的代碼,確實出現了一些錯誤。無論如何,我在這裏給你的佈局在我的模擬器上有效。你能否提供更多的代碼/上下文,以便我可以看到有什麼問題?

+0

你可能想看看http://stackoverflow.com/questions/2068007/android-how爲我的另一種解決方案獲取原始觸摸屏信息。 – 2010-05-30 16:20:05

+0

我試了一下你的代碼,並做了一些修正,但我可能無法讓它工作。感謝您抽出時間幫助我,即使我無法正常工作(這是我的錯,因爲我不知道Java) – Joe 2010-05-30 16:32:13

+0

非常感謝:)這件作品很好吃 – Joe 2010-05-30 19:12:17

3

聽起來好像你想從佈局的整個區域獲得觸摸事件來進行特定的測試。嘗試將觸摸監聽器附加到您的父視圖,而不是單獨的目標視圖。

這不是一個很常見的方法。在大多數情況下,您會希望在特定的子視圖中偵聽觸摸事件,並且如果在佈局中處理觸摸事件的其他視圖(例如按鈕),他們將優先於父視圖。有關處理UI事件的更多信息,請參閱http://developer.android.com/guide/topics/ui/ui-events.html

佈局(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> 

而在你的活動:

@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

進口什麼,我應該補充,因爲我剛剛收到錯誤:( 說明\t資源\t路徑\t位置\t類型 MotionEvent不能被解析爲一個類型\t AndroidTablet.java \t/AndroidTablet/src目錄/ joehollo/androidtablet \t線22 \t爪哇問題 R.layout.touch_viewer不能得到解決\t \t AndroidTablet.java/androidTablet/SRC/joehollo/androidtablet \t線17 \t爪哇問題 類型新View.OnTouchListener(){}必須實現繼承抽象方法查看.OnTouchListener.onTouch(View,MotionEv ent)\t AndroidTablet.java \t/AndroidTablet/src/joehollo/androidtablet \t line 21 \t Java問題 – Joe 2010-05-30 19:39:24

+0

+1這對我很好,謝謝 – 2013-07-10 19:53:04

0

使用觸摸屏上的噴氣男孩樣本Android應用程序下面的作品。 喬斯代碼與一個調整工作,使其閃耀背景。 我添加的唯一一條是

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

感謝喬(上圖)。

@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; 
     } 
    }); 
} 

佈局代碼

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

的更正沒有錯誤:

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