Android开发之触摸事件-点击屏幕获得屏幕的坐标

标签:
android安卓安卓触摸事件安卓获取屏幕坐标it |
分类: android |
http://s12/mw690/7256fe8fgd4a5d44cef7b&690
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
>
<TextView
android:id="@+id/info"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="点击获取屏幕的坐标" />
.xml文件
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
</LinearLayout>
.java文件
package
com.example.touchproject;
import
android.os.Bundle;
import
android.app.Activity;
import
android.view.Menu;
import
android.view.MotionEvent;
import
android.view.View;
import
android.view.View.OnTouchListener;
import
android.widget.TextView;
public class MainActivity
extends Activity {
private TextView
info=null;
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.info=(TextView)
super.findViewById(R.id.info);
//添加触摸事件
this.info.setOnTouchListener(new
TouchListenerImp());
}
private class TouchListenerImp
implements OnTouchListener{
public boolean onTouch(View v,
MotionEvent event) {
MainActivity.this.info.setText("x="+event.getX()+"y="+event.getY());
return false;
}
}
}