2012-04-12 115 views
1

所以我真的糊塗了,我有一個視圖(R.layout.main),其中包括自定義視圖(畫布)的Android的onClick只能使用一次

這個視圖包含了被重疊在畫布上的按鈕 但是當我點擊該按鈕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
static boolean jump=false; 

從這句話

boolean jump=false; 
+0

我需要那個satic,因爲這個變量用在canvas中 – user1293780 2012-04-12 11:12:04

+0

然後在canvas中訪問 後再次變爲false jump = false; – MAC 2012-04-12 11:40:03

0

你怎麼能肯定你的onClick被調用一次刪除靜態。在onClick方法中使用日誌打印消息來確保它被調用一次。你的代碼沒問題,我希望你的onClick能夠正常工作,並檢查你的其他代碼。

+0

好吧看起來你是正確的活動激發錯誤是在畫布上 – user1293780 2012-04-12 11:17:48