27

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

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

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

4

4 回答 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>

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

于 2010-05-30T16:15:11.437 回答
3

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;
        }
    });
}
于 2010-05-30T18:39:01.713 回答
0

以下工作使用 jetboy 示例 android 应用程序上的触摸屏。Joes 代码进行了一项调整,使其在背景中闪耀。我添加的唯一行是

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>
于 2010-12-29T03:28:38.583 回答
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;
    }
}
于 2014-03-07T15:47:39.147 回答