我是 Android 新手,我已经完成了 hello world 教程,并且对正在发生的事情有了基本的了解。我对 T-Mobile Pulse 的触摸屏特别感兴趣,所以为了让我开始,我希望能够在屏幕上写下 tocuh 事件的坐标,假设用户触摸了坐标 5 ,2 - 屏幕上的文本视图将显示该内容。
目前我有一个简单的程序,只加载一个 xml 文件,其中包含我打算在其中写入坐标的文本视图。
预先感谢您,我在 Google 上寻求帮助并搜索了 stackoverflow,但我发现的所有内容要么超出了我的想象,要么不适合于此。干杯。
您可以使用此功能: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)
您可能会大致这样将其放入 onCreate 方法中(这次测试):
活动 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>
Edit: 我尝试了我的代码,确实有一些错误。不管怎样,通过我在这里给你的布局,它可以在我的模拟器上运行。您能否提供更多代码/上下文,以便我看看出了什么问题?
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)