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

提前致谢!

答案

如果您检查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;

}

然后检查一下价值!

另一答案

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

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

String text = textView.getText().toString();

if(text.equals(text.toUpperCase()){

\The text is in Uppercase

}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐