如何在Android中设置按钮点击效果?

131

在安卓系统中,当我给按钮设置背景图片后,点击按钮时没有任何视觉效果。

我需要给按钮添加一些效果,以便用户能够识别出按钮已被点击。

当按钮被点击时应该暗淡几秒钟。如何实现这个效果?


https://dev59.com/aG445IYBdhLWcg3wpb8P#4755934 - ingsaurabh
以下方法允许您使用单个样式类来设置所有按钮的皮肤,而不管它们的文本是什么:https://dev59.com/C2435IYBdhLWcg3wlRMf#5327786 - Zack Marrapese
不,我没有使用图像按钮,我使用的是普通按钮,有什么简单的方法吗? - Jignesh Ansodariya
17个回答

3

我想再提供一种简单的方法来实现这个功能:如果你的ImageButton保留了它的背景,并且你没有将其设置为null,那么它就会像普通按钮一样工作,并在点击时显示与其他按钮完全相同的点击动画。隐藏背景的方法如下:

<ImageButton
    android:id="@+id/imageButton2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingBottom="1dp"
    android:paddingLeft="1dp"
    android:paddingRight="1dp"
    android:paddingTop="1dp"
    android:src="@drawable/squareicon" />

内边距会遮挡背景,使按钮表现得与其他按钮类似。


1
如果图像是圆形的,则此操作将使图像周围保留方形按钮边框。 - MSaudi

3
Step: set a button in XML with onClick Action:

 <Button
android:id="@+id/btnEditUserInfo"
style="?android:borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="@dimen/txt_height"
android:layout_gravity="center"
android:background="@drawable/round_btn"
android:contentDescription="@string/image_view"
android:onClick="edit_user_info"
android:text="Edit"
android:textColor="#000"
android:textSize="@dimen/login_textSize" />

Step: on button clicked show animation point
//pgrm mark ---- ---- ----- ---- ---- ----- ---- ---- -----  ---- ---- -----

    public void edit_user_info(View view) {

        // show click effect on button pressed
        final AlphaAnimation buttonClick = new AlphaAnimation(1F, 0.8F);
        view.startAnimation(buttonClick);

        Intent intent = new Intent(getApplicationContext(),  EditUserInfo.class);
        startActivity(intent);

    }// end edit_user_info

2
使用RippleDrawable实现Material Design的按下/点击效果。要实现这个效果,在/drawable文件夹下创建一个ripple item作为.xml文件,并在android:background中使用它来为任何视图设置背景。
- 对于图标按下/点击的效果,使用圆形涟漪效果,例如: - 对于带有矩形边界的视图点击效果,我们可以像下面这样在现有drawable上添加涟漪效果:

2

创建bounce.xml文件以进行动画

位置:

res->anim->bounce.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <scale
        android:duration="100"
        android:fromXScale="0.9"
        android:fromYScale="0.9"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1.0"
        android:toYScale="1.0" />

</set>

onClick中添加此行以进行初始化。
  final Animation myAnim = AnimationUtils.loadAnimation(this,R.anim.bounce);
         button.startAnimation(myAnim);

您可以在点击按钮时获得缩小效果。

0
做一个小的补充,关于Andràs的回答:
你可以使用postDelayed来使颜色过滤器持续一小段时间,以使其更加明显:
        @Override
        public boolean onTouch(final View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN: {
                    v.getBackground().setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
                    v.invalidate();
                    break;
                }
                case MotionEvent.ACTION_UP: {
                    v.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            v.getBackground().clearColorFilter();
                            v.invalidate();
                        }
                    }, 100L);
                    break;
                }
            }
            return false;
        }

您可以根据需要更改延迟100L的值。


0
如果您使用的是 XML 背景而不是 IMG,请删除此内容:
<item>
    <bitmap android:src="@drawable/YOURIMAGE"/>
</item>

从@Ljdawson给我们的第一个答案中得到。


0
我在安卓上尝试了一些设置按钮点击效果的方法: 1. 我创建了一个动画资源文件,用作按钮点击效果。 2. 创建了一个动画监听器。 3. 在动画结束时,我可以设置按钮点击后要启动的任务或活动。 4. 然后将动画设置到按钮上。
<scale
    android:duration="90"
    android:fromYScale="0.9"
    android:fromXScale="0.9"
    android:toYScale="1.0"
    android:toXScale="1.0"
    android:pivotY="50%"
    android:pivotX="50%"/>


Animation animationButton = AnimationUtils.loadAnimation(MyActivity.this,R.anim.bounce_button);

 animationButton.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }
            @Override
            public void onAnimationEnd(Animation animation) {
                Intent intent = new Intent(getApplicationContext(),SecondActivity.class);
                startActivity(intent);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                button.startAnimation(animationButton);


            }
        });

'''
I hope this will help.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接