Android onClick只能运行一次

时间:2012-04-12 11:04:26

标签: android button view onclick ontouchlistener

所以我真的很困惑 我有一个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>

2 个答案:

答案 0 :(得分:2)

static boolean jump=false;

从此声明中删除静态

boolean jump=false;

答案 1 :(得分:0)

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