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="textCapSentences|textMultiLine"

它还允许多线支持。

其他回答

对于EditText中的资本化,您可以选择以下两种输入类型:

android:inputType=“textCapSentences” android:inputType=“textCapWords”

textCapSentences 这将使每个句子中第一个单词的第一个字母成为大写字母。

textCapWords 这将使每个单词的第一个字母作为大写字母。

如果你想要两个属性都使用|符号

android:inputType="textCapSentences|textCapWords"

试试这个代码,它将大写所有单词的第一个字符。

-为EditText视图设置addTextChangedListener

edt_text.addTextChangedListener(观察者);

-添加文本观察者

TextWatcher watcher = new TextWatcher() {
    int mStart = 0;

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        mStart = start + count;
    }

    @Override
    public void afterTextChanged(Editable s) {
        String input = s.toString();
        String capitalizedText;
        if (input.length() < 1)
            capitalizedText = input;
        else if (input.length() > 1 && input.contains(" ")) {
            String fstr = input.substring(0, input.lastIndexOf(" ") + 1);
            if (fstr.length() == input.length()) {
                capitalizedText = fstr;
            } else {
                String sstr = input.substring(input.lastIndexOf(" ") + 1);
                sstr = sstr.substring(0, 1).toUpperCase() + sstr.substring(1);
                capitalizedText = fstr + sstr;
            }
        } else
            capitalizedText = input.substring(0, 1).toUpperCase() + input.substring(1);

        if (!capitalizedText.equals(edt_text.getText().toString())) {
            edt_text.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    edt_text.setSelection(mStart);
                    edt_text.removeTextChangedListener(this);
                }
            });
            edt_text.setText(capitalizedText);
        }
    }
};

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

android:inputType="textCapSentences|textMultiLine"

它还允许多线支持。

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

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

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

如果你想用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属性属性之间没有空格