Android开发之触摸事件-点击屏幕获…

来源:互联网 发布:python 期货 编辑:程序博客网 时间:2024/04/28 10:52
Android开发之触摸事件-点击屏幕获得屏幕的坐标

.xml文件
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  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="点击获取屏幕的坐标" />

</LinearLayout>

.java文件
packagecom.example.touchproject;

importandroid.os.Bundle;
importandroid.app.Activity;
importandroid.view.Menu;
importandroid.view.MotionEvent;
importandroid.view.View;
importandroid.view.View.OnTouchListener;
importandroid.widget.TextView;

public class MainActivityextends Activity {

private TextViewinfo=null;
protected void onCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.info=(TextView)super.findViewById(R.id.info);
//添加触摸事件
this.info.setOnTouchListener(newTouchListenerImp());
}
private class TouchListenerImpimplements OnTouchListener{

public boolean onTouch(View v,MotionEvent event) {
MainActivity.this.info.setText("x="+event.getX()+"y="+event.getY());
return false;
}
}
}
0 0
原创粉丝点击