如何在运行时检查Android中Edittext / TextView的textallcaps是true / false?

问题描述 投票:0回答:2

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

提前致谢!

android android-edittext android-textview android-textattributes
2个回答
3
投票

如果您检查TextView / Button源代码,找到here

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

然后检查一下价值!


2
投票

我不确定你为什么要检查它们,即使它确定它们会因为textAllCaps属性而处于大写状态。

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

String text = textView.getText().toString();
if(text.equals(text.toUpperCase()){
    \\The text is in Uppercase
}
© www.soinside.com 2019 - 2024. All rights reserved.