【问题标题】:Android detects clicks when it shouldn'tAndroid 在不应该检测到点击时检测到点击
【发布时间】:2014-01-13 13:20:19
【问题描述】:

我有一个图像按钮和相关的点击处理程序。

当我为按钮设置动画(使用平移动画)时,按钮显然会改变它在屏幕上的位置。

但是有一个问题:当我触摸初始按钮位置而不是当前位置时,Android 会检测到点击。

如何让 Android 尊重按钮的实际位置?

fragment_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

    <ImageButton
        android:id="@+id/addButton"
        android:src="@android:drawable/ic_input_add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="plusButtonClick"/>

</RelativeLayout>

MainActivity.java

public class MainActivity extends ActionBarActivity
{
    public void plusButtonClick(View v)
    {
        Toast.makeText(this, "Clicked!", Toast.LENGTH_SHORT).show();
        animateButton(R.id.addButton, R.anim.anim_move);
    }

    private void animateButton(int buttonId, int animationId)
    {
        View button = findViewById(buttonId);
        AnimationSet animationSet = (AnimationSet)AnimationUtils.loadAnimation(this, animationId);
        animationSet.setAnimationListener(getStartingButtonsListener(button));
        button.setVisibility(View.VISIBLE);
        button.startAnimation(animationSet);
    }

    private Animation.AnimationListener getStartingButtonsListener(final View v)
    {
        return new Animation.AnimationListener()
        {
            @Override
            public void onAnimationEnd(Animation arg0)
            {
                v.setVisibility(View.GONE);
            }
        };
    }
}

anim_move.xml

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:shareInterpolator="true"
     android:fillAfter="true"
     android:duration="1000">

    <translate
        android:fromXDelta="0"
        android:toXDelta="180"
        android:fromYDelta="0"
        android:toYDelta="0"/>
</set>

【问题讨论】:

    标签: android android-animation


    【解决方案1】:

    我在这里遇到了类似的问题:Button moves to different part of screen but can only press it at its initial spot?

    动画是动画像素,而不是小部件的触摸区域。 您还需要为属性设置动画:https://developer.android.com/guide/topics/graphics/prop-animation.html

    【讨论】:

      【解决方案2】:

      平移动画实际上并不移动您的对象,它只是移动它的图像。您应该在动画结束时自己重新定位按钮,方法是编辑其LayoutParams

      【讨论】:

        【解决方案3】:

        为您的按钮使用 onTouchEvent。此外,通过 java 文件给出这个 onTouch 实现,而不是 XML 布局文件。这比使用 onClick 事件要好得多。 看到这个:Triggering event when Button is pressed down in Android

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-07-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多