I'm working on a little personal todo list app and so far everything has been working quite well. There is one little quirk I'd like to figure out. Whenever I go to add a new item, I have a Dialog with an EditText view showing inside. When I select the EditText view, the keyboard comes up to enter text, as it should. In most applications, the default seems to be that the shift key is held for the first letter... although it does not do this for my view. There has to be a simple way to fix, but I've searched the reference repeatedly and cannot find it. I'm thinking there has to be an xml attribute for the reference loaded by the Adapter, but I can't find out what it is.


当前回答

我可以向你保证,两个答案的第一个字母大写,不会使编辑文本单行。

如果你想用XMl来做,下面是代码

android:inputType="textCapWords|textCapSentences"

如果想做它在活动/片段等下面是代码

momentTextView.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_WORDS | InputType.TYPE_TEXT_FLAG_MULTI_LINE)

PS:如果你有其他属性,你也可以很容易地添加一个管道“|”符号,只是确保在xml属性属性之间没有空格

其他回答

我创立和我的解决方案: 有两种解法 在爪哇:

 testEditText.setInputType(InputType.TYPE_CLASS_TEXT 
 | InputType.TYPE_TEXT_FLAG_CAP_WORDS);   

和XML:

 <EditText
android:id="@+id/mytxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textCapWords"
android:textSize="12sp" />

静态地(即在你的布局XML文件):设置android:inputType=" textcapsentence "在你的EditText。

编程上:你必须包括InputType。在EditText的InputType中的TYPE_CLASS_TEXT,例如

EditText editor = new EditText(this); 
editor.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);

可以与文本及其变体相结合,以要求每个句子的第一个字符大写。

-谷歌文档

在XML和JAVA文件中设置输入类型,

在XML中,

android:inputType=“textMultiLine|textCapSentences”

它也将允许多行和在JAVA文件,

edittext.setRawInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);

确保你键盘的自动大写设置是启用的。

只需在EditText元素中使用android:inputType="textCapWords"即可。

例如:

<EditText
    android:id="@+id/txtName"
    android:layout_width="0dp"
    android:layout_height="40dp"
    android:layout_weight="0.7"
    android:inputType="textCapWords"
    android:textColorHint="#aaa"
    android:hint="Name Surname"
    android:textSize="12sp" />

请参考以下链接: http://developer.android.com/reference/android/widget/TextView.html#attr_android%3ainputType

使用此代码仅第一个字母大写的EditText

MainActivity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:id="@+id/et"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:tag="true">
    </EditText>

</RelativeLayout>

MainActivity.java

EditText et = findViewById(R.id.et);
        et.addTextChangedListener(new TextWatcher() {
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2)
            {
                if (et.getText().toString().length() == 1 && et.getTag().toString().equals("true"))
                {
                    et.setTag("false");
                    et.setText(et.getText().toString().toUpperCase());
                    et.setSelection(et.getText().toString().length());
                }
                if(et.getText().toString().length() == 0)
                {
                    et.setTag("true");
                }
            }

            public void afterTextChanged(Editable editable) {

            }
        });