1

所以我真的很困惑我有一个包含自定义视图(画布)的视图(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;

从此语句中删除静态

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

你怎么能确定你的 onClick 被调用一次。在 onClick 方法中使用日志打印消息以确保它被调用一次。您的代码没问题,我希望您的 onClick 工作正常并检查您的其余代码。

于 2012-04-12T11:10:16.620 回答