如何使用Java模拟Android中的按钮单击?

问题描述 投票:0回答:1

[在我的Android应用中,我将让用户看到一个演示,该演示将在某些按钮上自动点击[模拟鼠标点击],我已经完成了上一个问题[How to simulate a delay?]的自动点击部分,但我想知道是否有一种方法可以在屏幕上显示按钮的颜色变化[当我模拟单击时],以便用户可以看到应该单击它,否则如果没有颜色变化,他们将如何知道单击了哪个按钮,所以我的问题是如何让按钮将其背景颜色更改为蓝色[或我喜欢的任何其他颜色],然后在0.3秒后更改回其原始颜色?我正在寻找一种方法,因此可以这样调用:

Button myButton = new Button(...);
int duration = 300;  // 300 ms
changeColor(myButton, Color.blue, duration);

在Android中如何实现这种效果?

android background-color android-button
1个回答
1
投票

好吧,您可以根据需要使用按钮的状态来更改颜色。对于这种方法,您将需要创建一个可绘制的xml文件。这样的事情(来自我的项目之一的示例):

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true" >
        <shape android:shape="rectangle"  >
            <solid android:color="@color/transparent_button_ripple_color"/>
        </shape>
    </item>
    <item android:state_focused="true">
        <shape android:shape="rectangle"  >
            <solid android:color="@color/transparent_button_ripple_color"/>
        </shape>
    </item>
    <item android:state_enabled="false">
        <shape android:shape="rectangle"  >
            <solid android:color="@color/transparent_button_ripple_color"/>
        </shape>
    </item>
    <item >
        <shape android:shape="rectangle"  >
            <gradient android:angle="-90" android:startColor="@color/colorTransparent" android:endColor="@color/colorTransparent" />
        </shape>
    </item>
</selector>

它们通过将状态设置为按钮来控制颜色。另外,为了表示在Android中的点击,您可以添加涟漪效果(仅供参考)。 https://guides.codepath.com/android/ripple-animation

编辑

我查看了您之前的问题,并针对您的代码创建了解决方案:

创建此方法:

private void updateViewColorWithDelay(View targetView) {
    targetView.setBackgroundColor(Color.parseColor("#000000"));
    targetView.postDelayed(new Runnable() {
        @Override
        public void run() {
            ivAvatar.setBackgroundColor(Color.parseColor("#ffffff"));
        }
    },300);
}

并与您的意见一起使用:

private class AutoDemoListener implements View.OnClickListener {
    public void onClick(View v) {
        Is_AutoDemo_B=true;
        Out("AutoDemoListener");
        switchView(demoView, registrationView);
        startRegistration();
        final Handler handler = new Handler();

        registrationView.symbolButton[2][8].performClick();
        updateViewColorWithDelay(registrationView.symbolButton[[2][8]);

        handler.postDelayed(new Runnable() {
            public void run() {
                registrationView.symbolButton[4][13].performClick();
                updateViewColorWithDelay(registrationView.symbolButton[4][13]);
            }
        }, 1000);

        handler.postDelayed(new Runnable() {
            public void run() {
                registrationView.symbolButton[0][1].performClick();
                updateViewColorWithDelay(registrationView.symbolButton[0][1]);
            }
        }, 3000);

        handler.postDelayed(new Runnable() {
            public void run() {
                registrationView.symbolButton[6][18].performClick();
                updateViewColorWithDelay(registrationView.symbolButton[6][18]);
            }
        }, 5000);

        handler.postDelayed(new Runnable() {
            public void run() {
                Is_AutoDemo_B=false;
            }
        }, 5100);

    }
}
© www.soinside.com 2019 - 2024. All rights reserved.