2012-04-12 94 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