有没有办法将TextView设置为大写的所有字母?

我希望能够将一个XML属性或样式分配给一个TextView ,它将使所有TextView字母的文本。

属性android:inputType="textCapCharacters"android:capitalize="characters"什么都不做,看起来像是用户input的文本,而不是TextView

我想这样做,所以我可以从内容中分离风格。 我知道我可以做这个程序化,但我希望保持风格的内容和代码。

我虽然这是一个非常合理的要求,但看起来你不能这样做。 多么彻底的失败 大声笑

更新

您现在可以使用textAllCaps来强制所有上限。

那么android:textAllCaps呢?

通过在Android应用中使用AppCompat textAllCaps (支持较旧的API)(小于14)

有一个与AppCompat一起提供的UI小部件,名为CompatTextView是一个自定义的TextView扩展,它增加了对textAllCaps

对于较新的android API> 14,你可以使用:

 android:textAllCaps="true" 

一个简单的例子:

 <android.support.v7.internal.widget.CompatTextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" app:textAllCaps="true"/> 

来源: developer.android

更新:

当它发生时,CompatTextView被最新的appcompat-v7库中的AppCompatTextView所取代〜Eugen Pechanec

使用样式( <item name="android:textAllCaps">true</item> )或者在具有textAllCaps属性的每个XML布局文件上执行操作真的非常令人失望,唯一的方法是实际上在使用textViewXXX.setText(theString)时,在每个string上使用String.toUpperCase()。

在我的情况下,我不想在我的代码中的任何地方都有theString.toUpperCase(),但有一个集中的地方去做,因为我有一些活动,并列出了TextView的布局,这些布局应该是大写的标题)和其他谁没有…所以…有些人可能会认为是一个矫枉过正,但我​​创build了我自己的CapitalizedTextView类扩展android.widget.TextView并覆盖setText方法大写文本dynamic。

至less,如果devise更改或我需要在未来的版本中删除大写的文本,我只需要在布局文件中更改为普通的TextView。

现在,考虑到我这样做是因为应用程序的devise者实际上想要在CAPS中的所有文本(标题)在整个应用程序,无论原来的内容大小,也有其他正常的TextViews的大写来与实际内容。

这是class级:

 package com.realactionsoft.android.widget; import android.content.Context; import android.util.AttributeSet; import android.view.ViewTreeObserver; import android.widget.TextView; public class CapitalizedTextView extends TextView implements ViewTreeObserver.OnPreDrawListener { public CapitalizedTextView(Context context) { super(context); } public CapitalizedTextView(Context context, AttributeSet attrs) { super(context, attrs); } public CapitalizedTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public void setText(CharSequence text, BufferType type) { super.setText(text.toString().toUpperCase(), type); } } 

只要你需要使用它,只需在XML布局中声明所有包:

 <com.realactionsoft.android.widget.CapitalizedTextView android:id="@+id/text_view_title" android:layout_width="wrap_content" android:layout_height="wrap_content" /> 

有些人会争辩说,在TextView上设置文本的正确方法是使用SpannableString ,但是我认为这会更加矫枉过正,更不用说耗费更多的资源,因为您将实例化另一个类而不是TextView。

我想出了一个类似于RacZo的解决scheme,我也创build了一个TextView的子TextView来处理文本的大写。

不同的是,我没有重写setText()方法之一,而是使用了类似的方法来实现TextView在API 14+上的实际操作(这是我认为更清晰的解决scheme)。

如果您查看源代码 ,您将看到setAllCaps()的实现:

 public void setAllCaps(boolean allCaps) { if (allCaps) { setTransformationMethod(new AllCapsTransformationMethod(getContext())); } else { setTransformationMethod(null); } } 

AllCapsTransformationMethod类不是(当前)公开的,但是源码也是可用的 。 我已经简化了这个类(删除了setLengthChangesAllowed()方法),所以完整的解决scheme是这样的:

 public class UpperCaseTextView extends TextView { public UpperCaseTextView(Context context) { super(context); setTransformationMethod(upperCaseTransformation); } public UpperCaseTextView(Context context, AttributeSet attrs) { super(context, attrs); setTransformationMethod(upperCaseTransformation); } public UpperCaseTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); setTransformationMethod(upperCaseTransformation); } private final TransformationMethod upperCaseTransformation = new TransformationMethod() { private final Locale locale = getResources().getConfiguration().locale; @Override public CharSequence getTransformation(CharSequence source, View view) { return source != null ? source.toString().toUpperCase(locale) : null; } @Override public void onFocusChanged(View view, CharSequence sourceText, boolean focused, int direction, Rect previouslyFocusedRect) {} }; } 

PixlUI项目允许您在textview的任何textview或子类中使用textAllCaps,包括:Button,EditText,AutoCompleteEditText,Checkbox RadioButton和其他几个。

您将需要使用pixlui版本创build您的textviews,而不是来自android源的文本,这意味着您必须这样做:

 <com.neopixl.pixlui.components.textview.TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" pixlui:textAllCaps="true" /> 

PixlUI还允许你设置一个自定义的字体/字体,你放在你的资产文件夹。

我正在使用Gradle 的PixlUI框架的Gradle 分叉,并且允许使用gradle来指定textAllCaps以及样式中的字体,而不是像原始项目那样需要内联。

似乎有移动键盘设置的权限,所以最简单的方法是:

 editText.setFilters(new InputFilter[]{new InputFilter.AllCaps()}); 

希望这会起作用