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文件,其中包含我打算编写坐标的textview。
Thank you in advance, I did Google for help and searched * but everything I found either went way over my head or wasn't suitable for this. Cheers.
提前谢谢你,我向谷歌寻求帮助并搜索了*,但我找到的所有内容都超出了我的想法,或者不适合这个。干杯。
4 个解决方案
#1
42
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
活动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;
}
});
}
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?
编辑:我尝试了我的代码,确实有一些错误。无论如何,我给你的布局在我的模拟器上工作。你能提供更多的代码/上下文,这样我就可以看出出了什么问题吗?
#2
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.
这不是一种非常常见的方法。在大多数情况下,您将希望在特定子视图中侦听触摸事件,如果您的布局中有其他视图处理触摸事件(例如按钮),则它们将优先于父视图。有关处理UI事件的详细信息,请参阅http://developer.android.com/guide/topics/ui/ui-events.html。
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;
}
});
}
#3
0
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>
#4
0
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;
}
}
#1
42
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
活动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;
}
});
}
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?
编辑:我尝试了我的代码,确实有一些错误。无论如何,我给你的布局在我的模拟器上工作。你能提供更多的代码/上下文,这样我就可以看出出了什么问题吗?
#2
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.
这不是一种非常常见的方法。在大多数情况下,您将希望在特定子视图中侦听触摸事件,如果您的布局中有其他视图处理触摸事件(例如按钮),则它们将优先于父视图。有关处理UI事件的详细信息,请参阅http://developer.android.com/guide/topics/ui/ui-events.html。
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;
}
});
}
#3
0
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>
#4
0
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;
}
}