【问题标题】:How to change button text color using Hexa-decimal color value?如何使用十六进制颜色值更改按钮文本颜色?
【发布时间】:2018-09-06 13:16:55
【问题描述】:

如何在单击按钮时使用十六进制值更改按钮文本颜色?我在 MainActivity 上使用以下代码,但无法正常工作

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

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

【问题讨论】:

    标签: java android kotlin android-button textcolor


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      试试这个:

       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 -->
      

      希望现在对你有帮助

      【讨论】:

        【解决方案3】:
        button.setTextColor(Color.parseColor("#ff0000")); 
        

        【讨论】:

          【解决方案4】:

          你可以这样使用

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

          【讨论】:

            【解决方案5】:

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

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

            【讨论】:

              【解决方案6】:

              可以有两种方式

              在 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"  /> 
                

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2012-11-04
                • 2021-07-19
                • 2014-08-07
                • 1970-01-01
                • 2016-09-25
                • 2016-04-02
                • 2019-06-16
                • 2021-03-08
                相关资源
                最近更新 更多