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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2939332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:58:41  来源:igfitidea点击:

Get the co-ordinates of a touch event on Android

androidtouchscreen

提问by Joe

I'm new to Android, I've followed the hello world tutorial through and have a basic idea of what's going on. I'm particularly interested in the touch screen of my T-Mobile Pulse so just to get me started I want to be able to write the co-ordinates of a tocuh event on the screen, so say the user touched the co-ordinate 5,2 - a textview on the screen would display that.

我是 Android 新手,我已经学习了 hello world 教程,并对正在发生的事情有一个基本的了解。我对我的 T-Mobile Pulse 的触摸屏特别感兴趣,所以为了让我开始,我希望能够在屏幕上写出 tocuh 事件的坐标,所以说用户触摸了坐标 5 ,2 - 屏幕上的文本视图将显示该内容。

At present I have a simple program that just loads an xml file which contains the textview I intend to write the co-ordinates in.

目前我有一个简单的程序,它只加载一个 xml 文件,其中包含我打算在其中写入坐标的文本视图。

Thank you in advance, I did Google for help and searched stackoverflow but everything I found either went way over my head or wasn't suitable for this. Cheers.

在此先感谢您,我使用 Google 寻求帮助并搜索了 stackoverflow,但我发现的所有内容要么超出我的头脑,要么不适合于此。干杯。

回答by Gautier Hayoun

You can use this function : http://developer.android.com/reference/android/view/View.html#setOnTouchListener(android.view.View.OnTouchListener)

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

You will probably put it in your onCreate method roughly this way (tested this time) :

您可能会大致以这种方式将其放入您的 onCreate 方法中(这次已测试):

Activity onCreate code

活动创建代码

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

layout code

布局代码

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

Edit: I tried my code and indeed there were a few errors. Anyway, with the layout I give you here it works on my emulator. Can you provide maybe more code/context so I can see what's wrong?

编辑:我尝试了我的代码,确实有一些错误。无论如何,使用我在这里提供的布局,它可以在我的模拟器上运行。你能否提供更多的代码/上下文,以便我看看有什么问题?

回答by adamp

It sounds like you want to get touch events from the whole area of your layout for this particular test. Try attaching the touch listener to your parent view rather than a separate target view.

听起来您想从整个布局区域获取此特定测试的触摸事件。尝试将触摸侦听器附加到您的父视图而不是单独的目标视图。

This isn't a very common approach. In most cases you will want to listen for touch events in a specific child view and if you have other views in your layout that handle touch events (such as a button) they'll take priority over a parent view. See http://developer.android.com/guide/topics/ui/ui-events.htmlfor more info about handling UI events.

这不是一种很常见的方法。在大多数情况下,您会希望在特定子视图中侦听触摸事件,并且如果您的布局中有其他视图处理触摸事件(例如按钮),它们将优先于父视图。有关处理 UI 事件的更多信息,请参阅http://developer.android.com/guide/topics/ui/ui-events.html

Layout (touch_viewer.xml):

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

And in your activity:

在您的活动中:

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

回答by adamp

The following works using a touch screen on the jetboy sample android app. Joes code worked with one tweak to make it shine the background thru. The only line I added was

以下工作在 jetboy 示例 android 应用程序上使用触摸屏。Joes 代码通过一项调整使其在背景中熠熠生辉。我添加的唯一一行是

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

Thanks Joe (above).

谢谢乔(上)。

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

layout code

布局代码

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

回答by Karthik S

The corrected version without the errors:

没有错误的更正版本:

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