Android based widget component to implement object move/control Drag function Example _android

Source: Internet
Author: User
Tags stub

The example in this article tells you that Android implements object move/control drag based on widget components. Share to everyone for your reference, specific as follows:

Package Com.sky;
Import android.app.Activity;
Import Android.os.Bundle;
Import android.view.KeyEvent;
Import android.view.MotionEvent;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.AbsoluteLayout;
Import Android.widget.Button;  @SuppressWarnings ("deprecation") public class Abosolutemove extends activity implements Onclicklistener {/** when The activity is the created.
 * * Public Button button1;
 Public Button button2;
 Public Button Button3;
 Public Button Button4;
 Button tmp;//temporarily save, select button int x;
 int y; The int flag;//is used to flag which button is selected @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (Savedinstan
    Cestate);
    Setcontentview (R.layout.main);
    button1= (Button) Findviewbyid (r.id.widget27);
    Button2= (Button) Findviewbyid (R.ID.WIDGET28);
    button3= (Button) Findviewbyid (r.id.widget29);
    button4= (Button) Findviewbyid (R.ID.WIDGET30);
    Button1.setonclicklistener (this); Button2.setonclickliStener (this);
    Button3.setonclicklistener (this);
  Button4.setonclicklistener (this); @Override public void OnClick (View v) {//TODO auto-generated Method Stub switch (V.getid ()) {case R.id.widget2
  7:flag=r.id.widget27;
  Settitle ("Button1");
  Break
  Case R.id.widget28:flag=r.id.widget28;
  Settitle ("Button2");
  Break
  Case r.id.widget29:flag=r.id.widget29;
  Settitle ("Button3");
  Break
  Case R.id.widget30:flag=r.id.widget30;
  Settitle ("Button4");
 Break @Override public boolean onKeyDown (int keycode, keyevent event) {//TODO auto-generated a stub return Supe
 R.onkeydown (KeyCode, event); @Override public boolean ontouchevent (Motionevent event) {//TODO auto-generated method Stub x = (int) Event.getx (
    );
    y = (int) event.gety (); tmp= (Button) Findviewbyid (flag);//Get selected Button absolutelayout.layoutparams params1=new absolutelayout.layoutparams
    (50,50,X-25,Y-50); Tmp.setlayoutparams (PARAMS1);/Set the new location of the button SWItch (Event.getaction ()) {case MotionEvent.ACTION_DOWN:tmp.invalidate ();
        Tmp.settext ("checked down");
      Break
       Case MotionEvent.ACTION_UP:tmp.invalidate ();
        Tmp.settext ("checked up");
      Break
       Case MotionEvent.ACTION_MOVE:tmp.invalidate ();
        Tmp.settext ("select Move");
    Break
 Return Super.ontouchevent (event);

 }
}

More interested readers of Android-related content can view this site: "Summary of Android Basic components Usage", "Introduction to Android Development and advanced Tutorials", "Android resource Operation Tips", "Android View tips Summary" and " A summary of the usage of Android controls

I hope this article will help you with the Android program.

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.