0

我已经为 TextView 设置了 android:textAllCaps="true" 以用大写字母显示标签。它的显示很好。但是,我需要在运行时检查标签是否为大写字母。但是,我找不到获取 textAllCaps 属性的属性。有人可以帮我吗?

提前致谢!

4

2 回答 2

3

如果您检查 TextView/ Button 源代码,请在此处找到

setAllCaps 方法使用该方法为对应的视图设置一个变换setTransformationMethod方法。

这是AllCapsTransformationMethod方法setAllCaps。因此,要获取是否设置了视图,您可以使用以下命令进行检查:

    TransformationMethod transformationMethod = btn.getTransformationMethod();
    if (transformationMethod!=null){
      if (transformationMethod.getClass().getSimpleName().equalsIgnoreCase(AllCapsTransformationMethod.class.getSimpleName())){
//        todo logic based code
      }

btn 是您的按钮视图

或者用它制作一个方法来检查 Button/TextView 是否设置了所有大写值

像这样的东西:

public boolean hasAllCaps(TextView textView){  
 TransformationMethod transformationMethod = textView.getTransformationMethod();
    if (transformationMethod!=null)
      if (transformationMethod.getClass().getSimpleName().equalsIgnoreCase(AllCapsTransformationMethod.class.getSimpleName())){
            return true;
      }
return false;
}

然后只需检查值!

于 2017-03-27T09:03:20.313 回答
2

我不确定您为什么要检查它们,即使由于textAllCaps属性可以肯定它们会大写。

一种解决方法是获取TextView的文本,然后将其与该文本的大写进行比较:

String text = textView.getText().toString();
if(text.equals(text.toUpperCase()){
    \\The text is in Uppercase
}
于 2017-03-27T07:08:36.383 回答