1

だから私はカスタムビュー(キャンバス)を含むView(R.layout.main)を持っているので本当に混乱しています

このビューには、キャンバス上にオーバーレイされるボタンが含まれていますが、ボタンをクリックすると OnClicklistener がイベントを発生させますが、その後、ボタンをクリックしても何もしません

アクティビティ :

public class RunActivity extends Activity implements OnTouchListener, OnClickListener {

    static int width;
    static int height;

    static boolean reset=false;

    //draw d;
    View d;

    Button jump_button;

    //jump
    float last_touchpos=0;
    static boolean jump=false;

    private static Context mContext;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

       //d = new draw(this);
        d = getLayoutInflater().inflate(R.layout.main, null);
        d.setOnTouchListener(this);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        mContext = this;

        //get screen size
        WindowManager wm = (WindowManager) this.getContext().getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();

        width = display.getWidth();  // deprecated
        height = display.getHeight();  // deprecated

        setContentView(d);

        jump_button = (Button) findViewById(R.id.jump);
        jump_button.setOnClickListener(this);
    }
    public static Context getContext(){
        return mContext;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        Log.d("touch","touched");

        if (draw.end == true)
        {
            reset=true;
        }
        else
        {
            if(last_touchpos != 0)
            {
                if(last_touchpos < event.getY())
                {
                    jump = true;
                    last_touchpos = 0;
                }
            }
            else
            {
                last_touchpos = event.getY();
            }
        }

        return false;
    }
    @Override
    public void onClick(View arg0) {
        jump = true;
    }
}

レイアウト :

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <run.alexander.fuchs.draw
        android:id="@+id/canvasview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <Button
        android:id="@+id/jump"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="Jump" />

</RelativeLayout>
4

2 に答える 2

2
static boolean jump=false;

このステートメントからstaticを削除します

boolean jump=false;
于 2012-04-12T11:09:21.737 に答える
0

onClick が一度呼び出されたことを確認するにはどうすればよいですか。onClick メソッド内でログ出力メッセージを使用して、一度呼び出されるようにします。あなたのコードは問題ありません。onClick が正しく動作し、残りのコードを確認してください。

于 2012-04-12T11:10:16.620 に答える