android 屏幕单击位置测试
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <!-- 定义TextView文本标签 --> <TextView android:id="@+id/Tv" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="点击屏幕得到相对屏幕的位置" /> </RelativeLayout>
package com.example.yanlei.yl; import android.graphics.Color; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.TextView; import android.text.Html; import android.text.Html.ImageGetter; import java.util.regex.Matcher; import java.util.regex.Pattern; import android.text.Editable; import android.text.TextWatcher; import android.widget.EditText; import android.widget.Button; import android.app.Activity; import android.content.Intent; import android.view.MotionEvent; import android.widget.TextView; public class MainActivity extends AppCompatActivity { //定义TextView对象 private TextView Tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //得到浏览器中的控件对象 findView(); } @Override public boolean onTouchEvent(MotionEvent event) { //当按下屏幕的时候,获取点击位置的x,y if (MotionEvent.ACTION_DOWN == event.getAction()) { float x = event.getX(); float y = event.getY(); Tv.setText("您点击的位置是:\nx:"+x+"\n y:"+y); } return super.onTouchEvent(event); } private void findView() { // 得到当前布局的控件对象 Tv = (TextView)findViewById(R.id.Tv); } }