首页 > 解决方案 > 当背景色调属性在按钮中为默认值时,如何更改android中单击按钮的背景颜色?

问题描述

      These are the attributes I have used in this button.
        I have also created one XML file button_round_corner_view.
           
           <Button
                        android:id="@+id/true_button"
                        android:layout_width="90dp"
                        android:layout_height="50dp"
                        android:layout_margin="10dp"
                        android:background="@drawable/button_round_corner_view"
                        android:padding="10dp"
                        android:text="True"
                        android:textColor="#000000"
                        android:textStyle="bold"
                        app:backgroundTint="#F8F4F4"
                        />
                
    This is the XML file attribute that I have created for a better button look.
please provide me the best answer for this problem and also tell me how to stop 

默认情况下应用于按钮的背景色调属性

   <?xml version="1.0" encoding="utf-8"?>
        <shape
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle">
        
            <corners
                android:radius="20dp"/>
        
            <!-- This is the border color -->
            <stroke
                android:width="2dp"
                android:color="@color/purple_700"/> 
      
            <!-- This is the background color -->
            <solid
                android:color="@color/white"/> 

        </shape>
                                                   

按钮中的 BackgroundTint 属性是默认值。我想在用户单击按钮时更改按钮的背景颜色。如何实现这一点。请帮我。

标签: javaandroid-studiobuttononclickbackground-color

解决方案


例如让我们说 button1 所以它会像

button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(Answer.equals("Correct")){
                button1.setBackgroundColor(mContext.getResources().getColor(R.color.DarkGreen));
                
            }else if(Answer.equals("InCorrect")){
                button1.setBackgroundColor(mContext.getResources().getColor(R.color.red));
            }
                
            }
        });

你可以做这样的事情。这就是改变按钮颜色的方式。


推荐阅读