SoFunction
Updated on 2025-04-07

Example of Android implementing object movement/control drag function based on widget component

This article describes the implementation of object movement/control dragging function based on widget components of Android. Share it for your reference, as follows:

package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
@SuppressWarnings("deprecation")
public class AbosoluteMove extends Activity implements OnClickListener
{
 /** Called when the activity is first created. */
 public Button button1;
 public Button button2;
 public Button button3;
 public Button button4;
 Button tmp;//Temporarily save, selected BUTTON int x;
 int y;
 int flag;// Used to select which BUTTON to select  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    (savedInstanceState);
    setContentView();
    button1=(Button) findViewById(.widget27);
    button2=(Button) findViewById(.widget28);
    button3=(Button) findViewById(.widget29);
    button4=(Button) findViewById(.widget30);
    (this);
    (this);
    (this);
    (this);
  }
 @Override
 public void onClick(View v)
 {
 // TODO Auto-generated method stub
 switch(())
 {
  case .widget27:
  flag=.widget27;
  setTitle("button1");
  break;
  case .widget28:
  flag=.widget28;
  setTitle("button2");
  break;
  case .widget29:
  flag=.widget29;
  setTitle("button3");
  break;
  case .widget30:
  flag=.widget30;
  setTitle("button4");
  break;
 }
 }
 @Override
 public boolean onKeyDown(int keyCode, KeyEvent event)
 {
 // TODO Auto-generated method stub
 return (keyCode, event);
 }
 @Override
 public boolean onTouchEvent(MotionEvent event)
 {
 // TODO Auto-generated method stub
 x = (int) ();
    y = (int) ();
    tmp=(Button) findViewById(flag);//Get the selected BUTTON     params1=new (50,50,x-25,y-50);
    (params1);//Set the new location of BUTTON    switch(())
    {
      case MotionEvent.ACTION_DOWN:
       ();
       ("Select down");
        break;
      case MotionEvent.ACTION_UP:
       ();
       ("Select UP");
        break;
      case MotionEvent.ACTION_MOVE:
       ();
       ("Select move");
        break;
    }
    return (event);
 }
}

For more information about Android related content, please check out the topic of this site:Summary of the usage of basic Android components》、《Android development introduction and advanced tutorial》、《Android resource operation skills summary》、《Android View View Tips Summary"and"Android control usage summary

I hope this article will be helpful to everyone's Android programming design.