如何使用十六进制颜色值更改按钮文本颜色?

How to change button text color using Hexa-decimal color value?

如何使用十六进制值更改单击按钮时的按钮文本颜色?我在 MainActivity 上使用下面的代码但没有工作

button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

           // button.setTextColor(Color.GRAY);
            button.setTextColor(Color.(#808080));
         }
    });

您应该使用 Color.parseColor 获取十六进制颜色字符串的 int 值,如下所示:

button.setTextColor(Color.parseColor("#808080"))
button.setTextColor(Color.parseColor("#ff0000")); 

你可以这样使用

button.setTextColor(Color.parseColor("#ff0000")); 

在 res/values/colors.xml 文件中定义颜色并像这样使用它:

button.setTextColor(getColor(R.color.your_color_name));

试试这个:

 button.setTextColor(Color.parseColor("#ff0000")); 

button.setTextColor(0xff0000); //SET CUSTOM COLOR

button.setTextColor(getApplication().getResources().getColor(R.color.red)); //TAKE DEFAULT COLOR

<Button android:id="@+id/btn_my_button" 
 android:text="YOUR_TEXT"  
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content"  
 android:textStyle="bold" 
 android:textColor="#ff0000" />  <-- SET TEXT COLOR HERE -->

希望现在对您有所帮助

可以有两种方式

在 res/values/colors 中定义您的颜色。xml

  1. 在您的 Activity.java

    上使用它

    button.setTextColor(getColor(R.color.defined_color_name));

  2. 在activity_name.xml

    上使用
            <Button    
            android:id="@+id/button"    
            android:layout_width="wrap_content"    
            android:layout_height="wrap_content"    
            android:text="My Button"    
            android:textColor="@color/nameOfYourColor"  />