获取Android上的触摸事件的坐标

问题描述:

我是Android新手,我跟随了hello world tutorial并对发生了什么有一个基本的想法。我对我的T-Mobile Pulse的触摸屏特别感兴趣,所以为了让我开始,我希望能够在屏幕上编写一个tocuh事件的坐标,所以说用户触摸了坐标5 ,2 - 屏幕上的文本视图会显示。获取Android上的触摸事件的坐标

目前我有一个只是用来加载包含我打算写的坐标在TextView的一个XML文件的简单程序。

预先感谢您,我做了谷歌的帮助和搜索计算器,但一切我发现要么走过我的头,要么不适合这个。干杯。

您可以使用此功能: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

听起来好像你想从布局的整个区域获得触摸事件来进行特定的测试。尝试将触摸监听器附加到您的父视图,而不是单独的目标视图。

这不是一个很常见的方法。在大多数情况下,您会希望在特定的子视图中侦听触摸事件,并且如果在布局中处理触摸事件的其他视图(例如按钮),他们将优先于父视图。有关处理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

使用触摸屏上的喷气男孩样本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> 

的更正没有错误:

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