<Relativelayoutxmlns: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" > <!--defining TextView Text Labels - <TextViewAndroid:id= "@+id/tv"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "tap the screen to get the position relative to the screen" /></Relativelayout>
PackageCom.example.yanlei.yl;ImportAndroid.graphics.Color;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.widget.TextView;Importandroid.text.Html;ImportAndroid.text.Html.ImageGetter;ImportJava.util.regex.Matcher;ImportJava.util.regex.Pattern;Importandroid.text.Editable;ImportAndroid.text.TextWatcher;ImportAndroid.widget.EditText;ImportAndroid.widget.Button;Importandroid.app.Activity;Importandroid.content.Intent;Importandroid.view.MotionEvent;ImportAndroid.widget.TextView; Public classMainactivityextendsappcompatactivity {//Defining TextView Objects PrivateTextView Tv; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); //get the control object in the browserFindview (); } @Override Public Booleanontouchevent (Motionevent event) {//when you press the screen, get the X, y of the click position if(Motionevent.action_down = =event.getaction ()) { floatx =Event.getx (); floaty =event.gety (); Tv.settext ("The location you clicked is: \nx:" +x+ "\ n y:" +y); } return Super. Ontouchevent (event); } Private voidFindview () {//gets the control object for the current layoutTv =(TextView) Findviewbyid (r.id.tv); }}
Android Screen click Location Test