我有两个按钮打包到同一个视图组中 - 一个简单的线性布局,如下所示:
<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);
&#34; circle_touch&#34;是一个简单的方法,它使用两个缩放动画(按下时进出缩放),如下所示:
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 :(得分: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>