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

27
我是新手学习 Android,我已经跟着“Hello World”教程走了一遍,并对正在进行的内容有了基本的了解。我特别感兴趣的是我的 T-Mobile Pulse 手机的触摸屏,为了让我入门,我想要能够在屏幕上写下一个触摸事件的坐标,就像用户触摸了坐标 5,2 - 一个文本视图会显示出来。
目前,我有一个简单的程序,只是加载了一个包含我打算写坐标的文本视图的 XML 文件。
提前感谢您的帮助,我在谷歌和 Stack Overflow 上搜索了帮助,但我找到的所有内容都要么超出了我的理解范围,要么不适合这个问题。谢谢。
4个回答

44
你可以使用这个函数:http://developer.android.com/reference/android/view/View.html#setOnTouchListener(android.view.View.OnTouchListener) 你可能会将它放在 onCreate 方法中,大致如下(已测试):
Activity 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>

编辑:我尝试了我的代码,确实有一些错误。无论如何,使用我在此处提供的布局,在我的模拟器上可以正常工作。你能否提供更多的代码/上下文,以便我可以看到问题出在哪里?


您可能需要查看https://dev59.com/vUvSa4cB1Zd3GeqPdlVo以获取另一种解决方案。 - Gautier Hayoun
我尝试了你的代码并进行了一些修改,但遗憾的是我无法使其正常工作。非常感谢你花时间帮助我,即使我无法让它正常工作(这是因为我不太了解Java的原因)。 - Joe
1
触摸监听器是否适用于滑动事件?也就是说,在滑动期间,触摸监听器是否会被重复调用? - Akas Antony

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

我应该添加哪些导入,因为我一直在收到错误 :(描述 资源路径 位置 类型 MotionEvent无法解析为类型 AndroidTablet.java /AndroidTablet/src/joehollo/androidtablet 第22行 Java问题 R.layout.touch_viewer无法解析 AndroidTablet.java /AndroidTablet/src/joehollo/androidtablet 第17行 Java问题 类型new View.OnTouchListener(){}必须实现继承的抽象方法View.OnTouchListener.onTouch(View, MotionEvent) AndroidTablet.java /AndroidTablet/src/joehollo/androidtablet 第21行 Java问题 - Joe

0
以下内容使用触摸屏在jetboy示例Android应用程序上运行。 Joe的代码经过一次微调就可以使它透过背景发光。 我添加的唯一一行是


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

感谢Joe(上面)。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接