EditText的textAllCaps属性在设置了textIsSelectable属性后无效。

5

我在布局中有一个TextView:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAllCaps="true"
    android:text="Hello"
    />

textAllCaps的效果很好,没有问题。但是当我尝试通过添加textIsSelectable使文本可选择时

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAllCaps="true"
    android:textIsSelectable="true"
    android:text="Hello"
    />

textAllCaps不起作用。似乎这两个属性无法正确地同时使用。你有什么建议,为什么会出现这种情况,如何使文本全大写且可选择(我感兴趣的是最清晰的方法,不是设置文本更改监听器和手动大写文本)。我将非常感激任何建议,谢谢。


setFocusableInTouchMode(true); --> setFocusableInTouchMode(true); setFocusable(true); --> setFocusable(true); - IntelliJ Amiya
你可以采用@Logic的答案,它对我有效。 - Garg
4个回答

6

您可以在 Activity 中尝试以下代码:

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

该代码可将输入框中的所有文本转换为大写字母。

2
我找到了一个解决方案。它是使用AppCompatTextViewapp:textAllCaps
<android.support.v7.widget.AppCompatTextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:textAllCaps="true"
    android:textIsSelectable="true"
    android:text="Hello"
    />

我不确定这是否是最好的解决方案(AppCompatTextView文档告诉我们只有在创建自定义组件时才使用此组件),但它可以工作。


0

您可以将inputType属性设置为"textCapCharacters",以默认使用大写键盘

<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textCapCharacters"
/>

0
use like this

<android.support.design.widget.TextInputLayout
                            android:id="@+id/input_postcode"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"

                            android:inputType="textCapCharacters"  //use this line dont forgot
                            android:textColorHint="@color/white"
                            app:hintTextAppearance="@style/TextAppearance.App.TextInputLayout">

                            <EditText
                                android:id="@+id/et_postcode"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_marginTop="10dp"
                                android:backgroundTint="@color/white"
                                android:gravity="center_vertical"
                                android:hint="Post code "
                                android:inputType="text" //dont use hereinputtype
                                android:imeOptions="actionNext"
                                android:maxLength="4"
                                android:padding="10dp"
                                android:textAllCaps="true"
                                android:singleLine="true"
                                android:textColor="@color/white"
                                android:textColorHint="@color/white"
                                android:textSize="15sp" />
                        </android.support.design.widget.TextInputLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接