0

我是一名 android 初学者程序员。我不知道如何处理 android 运动事件。任何人都可以帮助我使用以下代码。我想在 OnTouchListener.XML 和 .java 上获取 motionEvent 起始位置、结束位置已给出。在此先感谢。
xml代码:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/background2"
    android:gravity="top" >

    <TextView
        android:id="@+id/touch"
        android:background="#00000000"
        android:layout_width="350dp"
        android:layout_height="wrap_content"
        android:layout_above="@+id/tableRow1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/sidebar" />


< /RelativeLayout>

.java 代码:

package remote.bluefy.me;

import android.app.Activity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class Touchpad extends Activity{

    private Button lclick;
    private Button rclick;
    private View touch;
    private View side;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.touch);

        touch=(View)findViewById(R.id.touch);       

        touch.setOnTouchListener(new OnTouchListener(){

            public boolean onTouch(View v, MotionEvent event) {

                switch(event.getAction())
                {
                case MotionEvent.ACTION_DOWN:
                  //do something
                case MotionEvent.ACTION_MOVE:
                  //do something
                case MotionEvent.ACTION_CANCEL:
                  //do something
                }
                return false;
            }

        });


    }
}
4

1 回答 1

1

尝试使用这个

package remote.bluefy.me;

import android.app.Activity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class Touchpad extends Activity{

private Button lclick;
private Button rclick;
private View touch;
private View side;
private float startX, startY;
private float endX, endY;
private boolean isDown = false;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.touch);

    touch=(View)findViewById(R.id.touch);       

    touch.setOnTouchListener(new OnTouchListener(){

        public boolean onTouch(View v, MotionEvent event) {

            switch(event.getAction())
            {
            case MotionEvent.ACTION_DOWN:
                 if(isDown == false)
                 {
                 startX = event.getX();
                 startY = event.getY();
                 isDown = true;
                 }
            case MotionEvent.ACTION_UP:
                 endX = event.getX();
                 endY = event.getY();
            }
            return false;
        }

    });


}
}   
于 2012-07-15T13:37:30.480 回答