【问题标题】:Button click is not working after Animation动画后按钮单击不起作用
【发布时间】:2015-07-06 17:29:51
【问题描述】:

我在一个LinearLayout 中有两个button,在这个LinearLayout 里面我有两个按钮。当我为LinearLayout 设置动画时工作正常,但是当尝试单击LinearLayout 内的按钮时,单击button 不起作用。

我已经搜索了很多但无法摆脱这个问题。

下面的代码动画LinearLayout

an = new RotateAnimation(angleRightHead, getRotateAngleRightEdgehead (), Animation.RELATIVE_TO_SELF, 1, Animation.RELATIVE_TO_SELF, 0.33f);
an.setRepeatMode(Animation.REVERSE);
an.setDuration(1000);              
an.setRepeatCount(0);                
an.setFillAfter(true);   
an.setFillEnabled(true);   
head_neck_ll.startAnimation(an); 

还有这段 XML 代码

<LinearLayout  
    android:id="@+id/head_neck_idll"
    android:layout_width="wrap_content"
    android:layout_height="100dp"
    android:gravity="center"  
    android:orientation="horizontal" >    

    <Button
        android:id="@+id/head"
        android:layout_width="70dp"
        android:layout_height="20dp"
        android:background="@drawable/roundcorner"
        android:clickable="true"
        android:onClick="clickhander" />
     <!-- </RelativeLayout> -->

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:orientation="vertical" >

        <RelativeLayout
            android:layout_width="wrap_content"
            android:padding="3dp"
            android:layout_height="16dp" >

            <Button
                android:id="@+id/lumber"
                android:layout_width="49dp"
                android:layout_marginLeft="3dp"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerVertical="true"
                android:layout_marginBottom="-10dp"
                android:background="@drawable/roundcorner"
                android:clickable="true"
                android:onClick="clickhander" />

        </RelativeLayout>

        <Button
            android:id="@+id/backupper"
            android:layout_width="80dp"
            android:layout_height="20dp"
            android:background="@drawable/roundcorner"
            android:clickable="true"
            android:onClick="clickhander" />
    </LinearLayout>

</LinearLayout>

onClickListener的代码

public void clickhander(View v){
    if(v.getId() == R.id.head){
        textv = head;
        v1right = mapRight.get(head);
        v1right = mapLeft.get(head);

        head.setBackgroundResource(R.drawable.clickroundcorner);
        lumber.setBackgroundResource(R.drawable.roundcorner);
        backupper.setBackgroundResource(R.drawable.roundcorner);
        backlower.setBackgroundResource(R.drawable.roundcorner);    
        legside.setBackgroundResource(R.drawable.roundcorner);
        angle =0;
    } else if(v.getId() == R.id.backupper){
        textv = backupper;
        v1right = mapRight.get(backupper);
        v1right = mapLeft.get(backupper);

        backupper.setBackgroundResource(R.drawable.clickroundcorner);
        head.setBackgroundResource(R.drawable.roundcorner);   
        lumber.setBackgroundResource(R.drawable.roundcorner);
        backlower.setBackgroundResource(R.drawable.roundcorner);
        legside.setBackgroundResource(R.drawable.roundcorner);
        angle =0;
    }
}

查看图片:

注意:1 和 2 的布局相同。我想点击每个 layout animation 的监听器

您的帮助将不胜感激。

【问题讨论】:

  • 你能显示clickhander的代码吗?
  • 请尝试在动画完成后使用动画监听器清除动画。使用'clearAnimation()'。
  • 我认为问题在于动画会影响图像而不是按钮...您可以单击按钮所在的位置而不是应该在的位置吗?
  • @eduyayo.. 我也在努力解决同样的问题,但解决方案是什么?点击按钮从原来的位置工作...
  • @eduyayo 赞成亲爱的.. 你是对的吗?请理解问题然后上下投票。

标签: android rotation android-animation


【解决方案1】:

是的,这是正常行为。这是因为动画只是重新渲染View 的像素,但它在显示器上的位置保持不变。

如果你想将你的View重新定位到你的动画结束的地方,你需要调用View.layout()方法并传递这4个参数,它们描述了View在其布局上的新位置。

请记住,View.layout() 获取相对于 `View 的父级的参数。

【讨论】:

    猜你喜欢
    • 2012-10-30
    • 2011-12-25
    • 2015-07-23
    • 1970-01-01
    • 2021-02-08
    • 1970-01-01
    • 1970-01-01
    • 2019-12-01
    • 1970-01-01
    相关资源
    最近更新 更多