获取 Android 上触摸事件的坐标

发布于 2024-09-03 16:23:07 字数 282 浏览 8 评论 0原文

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

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

提前谢谢你,我在 Google 上寻求帮助并搜索了 stackoverflow,但我发现的所有内容都超出了我的想象或者不适合这个。干杯。

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.

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

ζ澈沫 2024-09-10 16:23:07

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

编辑:我尝试了我的代码,确实有一些错误。不管怎样,通过我在这里给你的布局,它可以在我的模拟器上运行。您能否提供更多代码/上下文,以便我看看出了什么问题?

You can use this function : 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) :

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?

风吹短裙飘 2024-09-10 16:23:07

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

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

布局(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;
        }
    });
}

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.html for more info about handling UI events.

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>

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;
        }
    });
}
假情假意假温柔 2024-09-10 16:23:07

以下是在 jetboy 示例 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>

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

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>
臻嫒无言 2024-09-10 16:23:07

没有错误的更正版本:

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

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文