2015-02-05 86 views
0

承認我有包裝到同一ViewGroup中兩個按鈕 - 一個簡單的線性佈局,像這樣:的Android onTouch其他視圖

 <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"> 

      <Button 
       android:id="@+id/activity_game_circle1" 
       android:layout_width="120dp" 
       android:layout_height="120dp" 
       android:text="1" 
       android:gravity="center" 
       android:textSize="36sp" 
       android:background="@drawable/blue_circle"/> 

      <Button 
       android:id="@+id/activity_game_circle2" 
       android:layout_width="120dp" 
       android:layout_height="120dp" 
       android:layout_marginLeft="16dp" 
       android:text="2" 
       android:gravity="center" 
       android:textSize="36sp" 
       android:background="@drawable/orange_circle"/> 
     </LinearLayout> 

在主活動我初始化我的按鍵和觸摸監聽器那樣的新

circle1 = (Button) findViewById(R.id.activity_game_circle1);       
    circle2 = (Button) findViewById(R.id.activity_game_circle2);       
    View.OnTouchListener touch_listener = new View.OnTouchListener() 
    { 
     @Override 
     public boolean onTouch(View view, MotionEvent motionEvent) 
     { 
      return circle_touch(view, motionEvent); 
     } 
    }; 
    circle1.setOnTouchListener(touch_listener); 
    circle2.setOnTouchListener(touch_listener); 

「circle_touch」是這樣的一種使用兩個縮放動畫(進出按下時縮放)的簡單方法:

public boolean circle_touch(View circle_view, MotionEvent event) 
    { 
     if (event.getAction() == MotionEvent.ACTION_DOWN) 
     { 
      // Animation in 
      circle_view.startAnimation(animation_in); 
     } 
     else if (event.getAction() == MotionEvent.ACTION_UP) 
     { 
      // Animation out 
      circle_view.startAnimation(animation_out); 
     } 

     return true; 
    } 

當我測試這段代碼時,出現了一個問題,在同一個按鈕上點擊兩次或三次後,另一個開始縮放,就像我觸摸它一樣,反之亦然。這是一個錯誤還是我錯過了什麼?

回答

0

所以我設法把按鈕具有相同父成單獨的父母來解決這個奇怪的現象,那就是將它們添加直線(或其他方式)的佈局是這樣的:

更新的XML:

<LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 

     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"> 

      <Button 
       android:id="@+id/activity_game_circle1" 
       android:layout_width="120dp" 
       android:layout_height="120dp" 
       android:text="1" 
       android:gravity="center" 
       android:textSize="36sp" 
       android:background="@drawable/blue_circle"/> 
     </LinearLayout> 
     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"> 
      <Button 
       android:id="@+id/activity_game_circle2" 
       android:layout_width="120dp" 
       android:layout_height="120dp" 
       android:layout_marginLeft="16dp" 
       android:text="2" 
       android:gravity="center" 
       android:textSize="36sp" 
       android:background="@drawable/orange_circle"/> 
     </LinearLayout> 
</LinearLayout>