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.


当前回答

只需在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

其他回答

在XML编辑文本中应用以下行。

android:inputType="textCapSentences|textMultiLine"

它还允许多线支持。

只需在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

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

或者android:inputType=" textcapsentence "将只工作,如果你的设备键盘自动大写设置启用。

要大写,您可以对编辑文本执行以下操作:

大写:使每个单词的第一个字母大写:

android:inputType="textCapWords"

使每个句子的第一个字母大写:

android:inputType="textCapSentences"

使每个字母大写:

android:inputType="textCapCharacters"

但这只会改变键盘和用户可以改变模式写小写字母。

因此,如果你真的想要大写格式的数据,这种方法不太受欢迎,首先添加以下类:

public class CapitalizeFirstLetter {
    public static String capitaliseName(String name) {
        String collect[] = name.split(" ");
        String returnName = "";
        for (int i = 0; i < collect.length; i++) {
            collect[i] = collect[i].trim().toLowerCase();
            if (collect[i].isEmpty() == false) {
                returnName = returnName + collect[i].substring(0, 1).toUpperCase() + collect[i].substring(1) + " ";
            }
        }
        return returnName.trim();
    }
    public static String capitaliseOnlyFirstLetter(String data)
    {
        return data.substring(0,1).toUpperCase()+data.substring(1);
    }
}

然后,

现在把每个单词都大写:

CapitalizeFirstLetter.capitaliseName(name);

只大写第一个词:

CapitalizeFirstLetter.capitaliseOnlyFirstLetter(data);

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

在XML中,

android:inputType=“textMultiLine|textCapSentences”

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

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

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