简体   繁体   English

当按钮中的背景色调属性为默认时,如何更改 android 中单击按钮的背景颜色?

[英]How to change the background color of a clicked button in android when background tint attribute is default in button?

      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 

the background tint attribute which applied by default on the button默认情况下应用于按钮的背景色调属性

   <?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 attribute in a button is the default.按钮中的 BackgroundTint 属性是默认值。 And I want to change the background color of my button when the user clicks the button.我想在用户单击按钮时更改按钮的背景颜色。 How to achieve this.如何实现这一点。 please help me.请帮我。

For Example Lets say there button1 so it will be like例如让我们说 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));
            }
                
            }
        });

You can do something like this.你可以做这样的事情。 This is how you change colour of a button.这就是改变按钮颜色的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM