【问题标题】:Get the co-ordinates of a touch event on Android获取 Android 上触摸事件的坐标
【发布时间】:2011-02-25 17:30:01
【问题描述】:

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

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

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

【问题讨论】:

    标签: android touchscreen


    【解决方案1】:

    没有错误的更正版本:

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

    【讨论】:

      【解决方案2】:

      以下操作使用 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>
      

      【讨论】:

        【解决方案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 line 17 Java 问题类型 new View.OnTouchListener(){} 必须实现继承的抽象方法 View.OnTouchListener.onTouch(View, MotionEvent) AndroidTablet .java /AndroidTablet/src/joehollo/androidtablet 第21行Java问题
        【解决方案4】:

        你可以使用这个功能: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>
        

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

        【讨论】:

        • 您可能需要查看stackoverflow.com/questions/2068007/… 以获得其他解决方案。
        • 我尝试了你的代码并做了一些我可以做的更正,但遗憾的是我无法让它工作。感谢您花时间帮助我,即使我无法让它工作(这是我不了解 Java 的错)
        • on touchListener 是否适用于滑动事件?意思是在滑动期间反复调用 touchListener 吗?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-03-28
        • 1970-01-01
        • 2012-03-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-05
        相关资源
        最近更新 更多