How to use Android Ontouchlistener

Source: Internet
Author: User

The Ontouchlistener interface is a listening interface for handling mobile screen events that is triggered when a touch is pressed, lifted, or slid within the scope of the view. The listener methods in this interface are signed as follows.

Java code:
public boolean OnTouch (View V, motionevent event)


       parameter V: Parameter V is also the event source object.
       parameter event: The parameter event encapsulates the object of the class, which encapsulates the details of the triggering event, including information such as the type of event, the trigger time, and so on.

       section describes a case where dragging a rectangle in the screen moves, this section will continue to use the case of the idea of listening to the interface to implement the drag button on the screen to move the case. The development steps are as follows.

       Create an Android project named Sample.

       Prepare the string resource, open the Strings.xml file, and replace the old code with the following code.

Java code:
<?xml version= "1.0" encoding= "Utf-8"?
<!--XML version and encoding--
<resources>
<string name= "Hello" >hello world, sample</string>
<!--define Hello string--
<string name= "App_name" >Sample</string>
<!--define App_name string--
<string name= "Location" > Locations </string>
<!--define a location string--
</resources>


Description: The string resource used in the program is defined in the same case as previously described.
Develop layout files. Open the Main.xml in the Res/layout directory, replacing its original code with the following code.

Java code:
<?xml version= "1.0" encoding= "Utf-8"?>
<!--the version and encoding of XML--
<absolutelayout
Android:id= "@+id/absolutelayout01"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
Xmlns:android= "Http://schemas.android.com/apk/res/android" >
<!--the version and encoding of XML--
<button
Android:layout_y= "123dip"
android:layout_x= "106dip"
android:text= "@string/location"
android:layout_height= "Wrap_content"
Android:id= "@+id/button01"
Android:layout_width= "Wrap_content"/>
<!--the version and encoding of XML--
</AbsoluteLayout>


Description: The layout file is very simple, just add a button control in an absolute layout, it should be noted that the button should be assigned an ID, so that the button can be referenced in Java code.

Next, start developing the main logic code. Write the Sample.java file with the code shown below.

Java code:
Package WYF.YTL;
The package where the declaration is
Import android.app.Activity;
Introducing related Classes
This section omits the introduction of some classes of code, readers can self-access to the book CD-ROM source \
Import Android.widget.Button;
Introducing related Classes
public class Sample extends Activity {
final static int wrap_content=-2;
Constant representing the Wrap_content
final static int x_modify=4;
Correction of x-coordinates in non-full-screen mode
final static int y_modify=52;
Correction of y-coordinates in non-full-screen mode
int Xspan;
Relative to the button's own coordinate system in case the stylus is clicked
int Yspan;
X, y location
public void OnCreate (Bundle savedinstancestate) {
Overriding the OnCreate method
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Setting the current user interface
Button bok= (button) This.findviewbyid (R.ID.BUTTON01);
Get a reference to a button
Bok.setontouchlistener (
Add Listener
New Ontouchlistener () {
Creating a Listener Class
public Boolean OnTouch (view view, motioneventevent) {
Overriding Listener methods
Switch (event.getaction ()) {
Monitoring Events
Case Motionevent.action_down:
Touch Pen Press
xspan= (int) event.getx ();
Get x Coordinate
yspan= (int) event.gety ();
Get the y-coordinate
Break
Case Motionevent.action_move:
Stylus Movement
Button bok= (button) Findviewbyid (R.ID.BUTTON01);
Let the button move with the stylus move
Viewgroup.layoutparams lp= New Absolutelayout.layoutparams (Wrap_content,
Wrap_content, (int) event.getrawx ()-xspan-x_modify, (int) Event.getrawy ()-yspan-y_modify);
BOK.SETLAYOUTPARAMS (LP);
Set the coordinates of a button
Break
}
return true;
}
}
);
}

public boolean onKeyDown (int keycode, keyevent event) {
Keyboard Keys Press the method
Button bok= (button) This.findviewbyid (R.ID.BUTTON01);
Get a reference to a button
Bok.settext (keycode+ "down");
Set the text of a button
return true;
}
public boolean onKeyUp (int keycode,keyevent event) {
How to lift the keyboard keys
Button bok= (button) This.findviewbyid (R.ID.BUTTON01);
Get a reference to a button
Bok.settext (keycode+ "up");
Set the text of a button
return true;
}
public boolean ontouchevent (motioneventevent) {
Let the button move along with the stylus's movement
Button bok= (button) This.findviewbyid (R.ID.BUTTON01);
Get a button reference
Viewgroup.layoutparams lp=
New Absolutelayout.layoutparams (
Create
Layoutparams wrap_content, wrap_content, (int) event.getrawx ()-xspan-x_modify,
X-coordinate
(int) Event.getrawy ()-yspan-y_modify
Y-coordinate
) ;
BOK.SETLAYOUTPARAMS (LP);
return true;
}

}


Line 6th to 10th declares some of the variables required in the program.
Line 11th to 40th overrides the OnCreate method in the activity, sets the current user interface in the method, and then gets a reference to the button and registers the listener for it. Line 16th to 38th creates the listener class and overrides the Ontouch method, and then performs different actions based on the type of event.
Line 41st to 45th overrides the onkeydown callback method, where you get a reference to the button and set the text on the button, line 46th to 50th overrides the onkeyup callback method, and the text on the button is also set.
Line 51st to 61st overrides the Ontouchevent callback method, which handles the listening method of the screen event, gets a reference to the button in the method, and then sets the coordinates of the button.

Run the case and drag the buttons on the screen to move through the pen.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.