您好,下午好,我需要SpannableString类的帮助,此类非常新,到目前为止,我已经在onCreate()方法中创建了这段代码:
String texto = "Texto para usar";
Spannable timeSpannable = new SpannableString(texto.toLowerCase());
boton.setText(timeSpannable);
这行不通,以确保在搜索解决方案时我在某个人的问题中使用了此论坛中的其他代码:
public static SpannableString highlight(String s, int k) {
SpannableString ss = new SpannableString(s);
ss.setSpan(new ForegroundColorSpan(Color.BLUE), 0, k, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
return ss;
}
之后,我调用onCreate()方法,因此我的程序如下所示:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout.LayoutParams layoutparams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
Button boton = (Button) findViewById(R.id.ir);
boton.setText("Texto para usar");
boton.setLayoutParams(layoutparams);
boton.setText(highlight(boton.getText().toString(),3));
}
这也不起作用,所以我的问题是“我做错了什么吗?当在任何API中使用SpannableString类时,是否存在任何类型的限制?¿最后我应该如何修改我的代码才能使其正常工作?
我在我的项目和我运行的Movil中使用api 15,以测试应用程序runns api 22:
顺便说一下,所有这些都是因为我想在我的appnote中设置一个搜索选项,以便当某人写一个像“ s”这样的字母时搜索所有“ s”字母并更改其颜色。
请注意,我已经在该论坛中搜索了2天之类的解决方案,因此,我在努力搜索解决方案后将其发布。
解决了问题。碰巧按钮的allCaps功能有问题。
这与以下事实结合在一起:自Android 5.0按钮以来,默认情况下已启用allCaps。
这将导致Spannable无法在按钮上使用。
为了解决这个问题,我找到了两种解决方案:
解决方案1
通过java从按钮中删除allCaps:
String texto = "Texto para usar";
Spannable timeSpannable = new SpannableString(texto);
boton.setAllCaps(false);
boton.setText(timeSpannable);
解决方案2
从按钮的布局中删除所有大写字母
<Button
...
android:allCaps="false" />
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句