Android learning [11] touch screen position control

The mobile phone screen event processing method onTouchEvent. This method is defined in the View class, and all View subclasses override this method, and the application can handle the touch events of the mobile phone screen through this method.

Its prototype is:

<span style="font-size:18px;">public boolean onTouchEvent(MotionEvent event)</span>  

Parameter event: The parameter event is an object of the mobile phone screen touch event encapsulation class, which encapsulates all the information of the event, such as the location of the touch, the type of the touch, and the time of the touch. This object is created when the user touches the phone screen.

Return value: The return value mechanism of this method is the same as that of the keyboard response event. It also returns true when the event has been completely processed and does not want other callback methods to process it again, otherwise it returns false.

This method does not handle only one event like the methods introduced before. Generally, the events in the following three cases are all handled by the onTouchEvent method, but the action values ​​in the three cases are different.

The screen is pressed: When the screen is pressed, this method will be automatically called to process the event. At this time, the value of MotionEvent.getAction() is MotionEvent.ACTION_DOWN. If the application needs to handle the event of the screen being pressed, only The callback method needs to be reset, and then the action judgment can be performed in the method.

The screen is lifted: an event triggered when the stylus leaves the screen. This event also needs to be captured by the onTouchEvent method, and then the action judgment is performed in the method. When the value of MotionEvent.getAction() is MotionEvent.ACTION_UP, it means that the screen is lifted.

Drag in the screen: This method is also responsible for handling the event of the stylus sliding on the screen. It also calls the MotionEvent.getAction() method to determine whether the action value is MotionEvent.ACTION_MOVE before processing.

The sample code is as follows: MainActivity.java

<span style="font-size:18px;">package com.example.touchpostionshow;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.widget.EditText;

public class MainActivity extends Activity {
	public EditText pox,poY,condition;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate (savedInstanceState);
		setContentView(R.layout.activity_main);
		 pox = (EditText)findViewById(R.id.editText1);
		 poY = (EditText)findViewById(R.id.editText2);
		 condition = (EditText)findViewById(R.id.editText3);
		
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	@Override
	public boolean onTouchEvent(MotionEvent event)
	{
		float x = event.getX();
		float y = event.getY();
		try
		{
			switch(event.getAction())
			{
				case MotionEvent.ACTION_DOWN: pox.setText(""+x);poY.setText(""+y);condition.setText("down");break;
				case MotionEvent.ACTION_UP:pox.setText(""+x);poY.setText(""+y);condition.setText("up");break;
				case MotionEvent.ACTION_MOVE:pox.setText(""+x);poY.setText(""+y);condition.setText("move");break;
			}
			return true;
		}
		catch(Exception e)
		{
			Log.v("touch", e.toString());
			return false;
		}
	}

}
</span>
Add three edit text boxes to the XML file to display the XY of the coordinates and whether the finger is pressed and lifted or moved.


Original click here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325758522&siteId=291194637