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

cot*_*aws 227 android capitalization textview

我希望能够将xml属性或样式分配给一个TextView将在所有大写字母中生成任何文本的属性或样式.

属性android:inputType="textCapCharacters"并且android:capitalize="characters"什么也不做,看起来像是用户输入的文本,而不是TextView.

我想这样做,所以我可以将风格与内容分开.我知道我可以在程序上做到这一点,但我想再次保持内容和代码的风格.

Ter*_*nce 337

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

更新

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

  • textview属性肯定缺乏.也很高兴从属性文件夹中分配一个ttf! (14认同)
  • 链接问题的答案之一暗示'android:textAllCaps ="true"'这对我有用. (7认同)

Mur*_*phy 138

那么android:textAllCaps呢?

  • @ larham1这是一个xml属性,所以我看不出它是如何使用它的.如果不工作,请向Google报告.我认为它仅限于API级别14,如setAllCaps(boolean). (8认同)

LOG*_*TAG 61

在支持旧版API的Android应用中使用AppCompat textAllCaps(少于14个)

AppCompat附带了一个名为CompatTextView的UI小部件,它是一个Custom TextView扩展,增加了对textAllCaps的支持

对于较新的Android API> 14,您可以使用:

android:textAllCaps="true"
Run Code Online (Sandbox Code Playgroud)

一个简单的例子:

<android.support.v7.internal.widget.CompatTextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:textAllCaps="true"/>
Run Code Online (Sandbox Code Playgroud)

资料来源:developer.android

更新:

在最新的appcompat-v7库中, CompatTextView被AppCompatTextView取代了~Eugen Pechanec

  • @EmanuelMoecklin在最新的appcompat-v7库中,`CompatTextView`被`AppCompatTextView`取代了.你说的很对. (2认同)

Osc*_* S. 30

非常令人失望的是你无法使用styles(<item name="android:textAllCaps">true</item>)或使用textAllCaps 属性在每个XML布局文件上执行它,并且实现它的唯一方法实际上是在每个字符串上使用String.toUpperCase() textViewXXX.setText(theString).

在我的情况下,我不想在我的代码中到处都有theString.toUpperCase(),但是有一个集中的地方可以做到这一点,因为我有一些活动并列出了TextViews的项目布局,其中应该始终大写(a)标题)和其他没有...所以...有些人可能认为是一种矫枉过正,但我​​创建了自己的CapitalizedTextView类,扩展了android.widget.TextView并覆盖了动态文本大写的setText方法.

至少,如果设计更改或我需要在将来的版本中删除大写文本,我只需要在布局文件中更改为普通的TextView.

现在,考虑到我这样做是因为App的设计者实际上想要在整个应用程序中使用CAPS中的这个文本(标题),无论原始内容大小写,还有其他普通的TextViews,其中大写字母带有实际内容.

这是班级:

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

}
Run Code Online (Sandbox Code Playgroud)

无论何时需要使用它,只需使用XML布局中的所有包声明它:

<com.realactionsoft.android.widget.CapitalizedTextView 
        android:id="@+id/text_view_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
Run Code Online (Sandbox Code Playgroud)

有人会争辩说,在TextView上设置文本样式的正确方法是使用SpannableString,但我认为这甚至可能是更大的过度杀伤,更不用说更耗费资源了,因为你将实例化另一个类而不是TextView.


Nat*_*tix 9

我想出了一个类似于RacZo的解决方案,因为我还创建了一个子类,TextView它处理文本大写.

不同之处在于setText(),我没有覆盖其中一种方法,而是采用了类似的方法来TextView实现API 14+ 的实际操作(在我看来,这是一种更清洁的解决方案).

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

public void setAllCaps(boolean allCaps) {
    if (allCaps) {
        setTransformationMethod(new AllCapsTransformationMethod(getContext()));
    } else {
        setTransformationMethod(null);
    }
}
Run Code Online (Sandbox Code Playgroud)

AllCapsTransformationMethod课程(目前)不是公开的,但仍然可以使用该课程.我已经简化了那个类(删除了setLengthChangesAllowed()方法),所以完整的解决方案是这样的:

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) {}
    };
}
Run Code Online (Sandbox Code Playgroud)


can*_*ler 9

基本上,在 XML 文件的 TextView 中写入以下内容:

android:textAllCaps="true"
Run Code Online (Sandbox Code Playgroud)