單指觸控拖曳1個textView物件:使用onTouchEvent方法 注意:整個事件程式碼都必須放在 onCreate() { } 下面 .......................... 範例: import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.widget.TextView; import android.widget.AbsoluteLayout.LayoutParams; import android.widget.*; import android.view.*; private float touchX; private float touchY; private int tvWidth = LayoutParams.WRAP_CONTENT; private int tvHeight = LayoutParams.WRAP_CONTENT; private TextView tv = null; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 取得 TextView 物件 tv = (TextView)findViewById(R.id.textView1); } // 利用 MotionEvent 處理觸控程序 public boolean onTouchEvent(MotionEvent event) { touchX = event.getX(); // 觸控的 X 軸位置 touchY = event.getY() - 50; // 觸控的 Y 軸位置 // 判斷觸控動作 switch( event.getAction() ) { case MotionEvent.ACTION_DOWN: // 按下 tv.setLayoutParams(new AbsoluteLayout.LayoutParams( tvWidth, tvHeight, (int)touchX, (int)touchY)); break; case MotionEvent.ACTION_MOVE: // 拖曳移動 tv.setLayoutParams( new AbsoluteLayout.LayoutParams( tvWidth, tvHeight, (int)touchX, (int)touchY)); break; case MotionEvent.ACTION_UP: // 放開 break; } return super.onTouchEvent(event); }//end of onTouchEvent()