点击后如何临时更改Android按钮样式?

How to change Android button style temporarily after click?

请建议我一些好的做法来更改按钮背景颜色 onclick(几秒钟)。我用 Android API 22.

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

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);
    }
}

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

祝你有愉快的一天!

我为 Android API 21 提出了这个解决方案:

@Override
public void onClick(final View view) {
    final int redColor = 0xFFFF0000;
    view.getBackground().setColorFilter(redColor, PorterDuff.Mode.MULTIPLY);
    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            view.getBackground().clearColorFilter();
        }
    }, 700);
}