如何在Android中点击按钮时更改按钮的颜色?

90

我正在开发Android应用程序。我想在屏幕底部横向放置4个按钮。其中2个按钮上有图像。按钮的边框颜色应为黑色,边框应尽可能细。当我单击按钮时,我希望按钮的背景色更改为蓝色,而不更改边框的颜色,并保持该颜色一段时间。如何在Android中实现这种情况?


可能是[如何在按钮点击时更改矢量可绘制路径的颜色]的重复问题(https://dev59.com/x1sV5IYBdhLWcg3w6ych)我已将其标记为重复,您可以在此处检查我的答案:https://stackoverflow.com/a/55205149/3763032 - mochadwi
请添加android:background="?android:attr/selectableItemBackground"。如果可以的话,请在下方投票支持我的回答,谢谢。 - Ashraf Amin
16个回答

1
public void onPressed(Button button, int drawable) {
            if (!isPressed) {
                button.setBackgroundResource(R.drawable.bg_circle);
                isPressed = true;
            } else {
                button.setBackgroundResource(drawable);
                isPressed = false;
            }
        }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.circle1:
                onPressed(circle1, R.drawable.bg_circle_gradient);
                break;
            case R.id.circle2:
                onPressed(circle2, R.drawable.bg_circle2_gradient);
                break;
            case R.id.circle3:
                onPressed(circle3, R.drawable.bg_circle_gradient3);
                break;
            case R.id.circle4:
                onPressed(circle4, R.drawable.bg_circle4_gradient);
                break;
            case R.id.circle5:
                onPressed(circle5, R.drawable.bg_circle5_gradient);
                break;
            case R.id.circle6:
                onPressed(circle6, R.drawable.bg_circle_gradient);
                break;
            case R.id.circle7:
                onPressed(circle7, R.drawable.bg_circle4_gradient);
                break;

        }

请尝试这个,在这段代码中,我试图在按钮点击时更改按钮的背景色,这可以正常工作。

0
即便是使用了上面的某些评论,这个问题的解决时间比应该需要的长得多。希望这个例子能对其他人有所帮助。
在drawable目录中创建radio_button.xml文件。
    <?xml version="1.0" encoding="utf-8"?>

<!-- An element which allows two drawable items to be listed.-->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:drawable="@drawable/radio_button_checked" /> <!--pressed -->
    <item android:drawable="@drawable/radio_button_unchecked" /> <!-- Normal -->
</selector>

然后在片段的xml中应该看起来像这样

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent">

<RadioButton

    android:id="@+id/radioButton1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_weight="1"
    android:button="@drawable/radio_button"
    android:paddingLeft="10dp" />        
<RadioButton
    android:id="@+id/radioButton2"
    android:layout_marginLeft="10dp"
    android:paddingLeft="10dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:button="@drawable/radio_button" />

</RadioGroup>
    </LinearLayout>
</layout>

0

要正确处理您希望按钮保持其他颜色的时间,我建议使用此版本:

button.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch(event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        button.setBackground(getResources().getDrawable(R.drawable.on_click_drawable));
                        break;
                    case MotionEvent.ACTION_UP:
                        new java.util.Timer().schedule(
                                new java.util.TimerTask() {
                                    @Override
                                    public void run() {
                                        ((Activity) (getContext())).runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {
                                                button.setBackground(getResources().getDrawable(R.drawable.not_clicked_drawable));
                                            }
                                        });
                                    }
                                }, BUTTON_CLICK_TIME_AFTER_RELEASE_ANIMATION);
                        break;
                    default:
                }
                return false;
            }
        });

BUTTON_CLICK_TIME_AFTER_RELEASE_ANIMATION 表示按钮在释放后多长时间 [毫秒] 将重置为先前的状态(但是,根据您想要实现的内容,您可能希望使用一些布尔值来检查按钮是否在此期间已被使用...)。


0

只需在 XML 中添加一行代码即可实现点击项:

android:background="?android:attr/selectableItemBackground"


0
 android:focusableInTouchMode="true"

Then

<item android:drawable="@drawable/bottom_line_green" 
android:state_focused="true"></item>
<item android:drawable="@color/white"  
android:state_focused="false"></item>

-1

这是最简单的方法:

将此代码添加到mainactivity.java中

public void start(View view) {

  stop.setBackgroundResource(R.color.red);
 start.setBackgroundResource(R.color.yellow);
}

public void stop(View view) {
stop.setBackgroundResource(R.color.yellow);
start.setBackgroundResource(R.color.red);
}

然后在你的活动主函数中

    <button android:id="@+id/start" android:layout_height="wrap_content" android:layout_width="wrap_content" android:onclick="start" android:text="Click">

</button><button android:id="@+id/stop" android:layout_height="wrap_content" android:layout_width="wrap_content" android:onclick="stop" android:text="Click">

或者跟随这个教程一起学习


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