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 に答える