点击后如何临时更改Android按钮样式? [英] How to change Android button style temporarily after click?

查看:52
本文介绍了点击后如何临时更改Android按钮样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请给我建议一些好的做法,以更改onclick的按钮背景色(持续几秒钟).我使用的是Android API 22.

Please, suggest me some good practice to change button background color onclick(for few seconds). I use Android API 22.

推荐答案

几天前,我遇到了类似的问题,请随时使用我的代码.

I had a similar issue a few days back, so feel free to use my code.

Button myButton; //as a "global" variable so that it is also recognized in the onClick event.

myButton = (Button) findViewById(R.id.b)
myButton.setBackgroundColor(Color.BLACK); //set the color to black
myButton.setOnClickListener(new View.OnClickListener() {        
    @Override
    public void onClick(View v) {
        myButton.setBackgroundColor(Color.RED); //set the color to red
        // Delay of 2 seconds (200 ms) before changing back the color to black
        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                myButton.setBackgroundColor(Color.BLACK); //set the color to black
            }
        }, 200);
    }
}

我不知道这是否被认为是好的做法...

I don't know if this is considered good practice though...

祝你有美好的一天!

这篇关于点击后如何临时更改Android按钮样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆