【问题标题】:How to simulate a button click in Android with Java?如何用Java模拟Android中的按钮点击?
【发布时间】:2020-04-27 04:23:10
【问题描述】:

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

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

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

【问题讨论】:

  • 我相信this 会是最简单的方法

标签: android background-color android-button


【解决方案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);

    }
}

【讨论】:

  • 我所有的按钮都是由我的Java程序生成的,如何在不使用xml的情况下通过程序来实现?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-20
  • 2015-01-30
  • 2015-06-28
相关资源
最近更新 更多