【问题标题】:Displaying button's text with two different colors用两种不同的颜色显示按钮的文本
【发布时间】:2018-02-10 09:26:14
【问题描述】:

所以我可以使用不同的颜色显示 textView 的文本

String redString= "<font color='#ee0000'>yay</font>";
String blackString = "<font color='#000000'>eureka</font>";
textView.setText(Html.fromHtml(redString + blackString));

但是当试图做同样的事情时

button.setText(...)

它不起作用!你知道按钮的文本是否可以有不同的颜色吗?如果没有,是否有其他解决方案?

编辑:我尝试了建议的两种解决方案,但都没有奏效。为了确定,我创建了一个新项目:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //first solution
    String redString= "<font color='#ee0000'>yay </font>";
    String blackString = "<font color='#000000'>eureka</font>";
    TextView test1 = (TextView) findViewById(R.id.textView);
    Button test2 = (Button) findViewById(R.id.button2);
    test1.setText(Html.fromHtml(redString + blackString));
    test2.setText(Html.fromHtml(redString + blackString));

    //second solution
    Button b = (Button) findViewById(R.id.button);
    SpannableString text = new SpannableString("Hello World");
    text.setSpan(new ForegroundColorSpan(Color.RED), 0, 4, 0);
    text.setSpan(new ForegroundColorSpan(Color.BLUE), 5, text.length(), 0);
    b.setText(text, TextView.BufferType.SPANNABLE);
    }
}

And this is the result(抱歉,由于信誉低,无法显示图片)

我在 Windows 10 上使用 AS 2.3.3。模拟器使用带有 API 25 的 Android 7。感谢您的帮助!

【问题讨论】:

  • 您可以使用简单的线性布局(水平),将文本视图作为按钮并在此线性布局上使用 setOnClickListener。
  • 检查 SpannableString
  • 可能的解决方案是here。最好的问候
  • 请描述... it doesn't work!

标签: java android button text colors


【解决方案1】:

它对我有用...

TextView test = (TextView) findViewById(R.id.ettx);

        Button test2 = (Button) findViewById(R.id.ettxb);

        String redString= "<font color='#ee0000'>yay</font>";
        String blackString = "<font color='#000000'>eureka</font>";
        test.setText(Html.fromHtml(redString + blackString));

        test2.setText(Html.fromHtml(redString + blackString));

【讨论】:

    【解决方案2】:

    使用此代码

    Button b = (Button) findViewById(R.id.button1);
    SpannableString text = new SpannableString("Hello World");
    
    text.setSpan(new ForegroundColorSpan(Color.RED), 0, 4, 0);
    
    text.setSpan(new ForegroundColorSpan(Color.BLUE), 5, text.length(), 0);
    
    b.setText(text, BufferType.SPANNABLE);
    

    【讨论】:

    • 试过了还是不行,按钮的文字颜色还是默认的!导致编辑部分
    【解决方案3】:

    如果您的按钮有android:textAllCaps = true,它可能不起作用。尝试将其设置为false

    【讨论】:

      【解决方案4】:

      试试这个:

      TextView b = (TextView) findViewById(R.id.text);
      
      SpannableString text = new SpannableString("Color Change");
      
      text.setSpan(new ForegroundColorSpan(Color.BLUE), 0, 5, 0);
      
      text.setSpan(new ForegroundColorSpan(Color.GREEN), 5, text.length(), 0);
      
      b.setText(text, TextView.BufferType.SPANNABLE);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-09-24
        • 1970-01-01
        • 2012-07-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多