我要求Edittext
所有单词都以大写字母开头。如果用户将其写入较小的情况(单词的第一个字母),那么它也应该将其转换为Uppercase
。
到目前为止,我已经在布局中完成了它:
<EditText
android:id="@+id/edGymName"
style="@style/LoginRegisterEditText"
android:layout_marginTop="@dimen/size_10"
android:layout_toLeftOf="@+id/txtStatusGymStatus"
android:hint="@string/gym_tag"
android:inputType="textPersonName|textCapWords|textNoSuggestions"
android:maxLength="30" />
但是,我不想让用户在小写字母中写下单词的第一个字母。这是有效的,但用户可以在small case
中写出单词的第一个字母。如果我们强行不允许它会怎么样。
答案 0 :(得分:1)
将input type
设置为TYPE_CLASS_TEXT| TYPE_TEXT_FLAG_CAP_CHARACTERS.
android:inputType="textCapCharacters"
android:inputType="textCapSentences"
为感官
每个单词android:inputType="textCapWords"
答案 1 :(得分:1)
将输入类型更改为 TYPE_CLASS_TEXT |的输入类型TYPE_TEXT_FLAG_CAP_CHARACTERS 强>
IQueryable
或来自java代码
android:inputType="text|textCapCharacters"
答案 2 :(得分:1)
静态(即在您的布局XML文件中):set
android:inputType="textCapSentences" on your EditText.
以编程方式:您必须在EditText的InputType中包含InputType.TYPE_CLASS_TEXT
,例如
EditText editor = new EditText(this);
editor.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);
用户可以手动更改Soft keyBoard中的文本大写以管理这种情况,您可以设置输入过滤器。 Android为此提供了AllCap过滤器。
edittext.setFilters(new InputFilter[] {new InputFilter.AllCaps()});
设置过滤器将重置您在清单中设置的其他一些属性。所以要小心。就像你在xml中设置了maxlenth
属性一样,然后在设置过滤器之后你需要在运行时重置它,否则它不会起作用。以下是例子。
editText.setFilters(new InputFilter[] {new InputFilter.AllCaps(),new InputFilter.LengthFilter(40)});
所以最好的方法防止所有以前的过滤器,只需添加一个新的。
InputFilter[] oldFilters = editText.getFilters();
InputFilter[] newFilters = new InputFilter[oldFilters.length + 1];
System.arraycopy(oldFilters, 0, newFilters, 0, oldFilters.length);
newFilters[oldFilters.length] = new InputFilter.AllCaps();
editText.setFilters(newFilters);
答案 3 :(得分:1)
像这样设置Edittext:
EditText edit = (EditText)findViewById(R.id.myEditText);
String input;
....
input = edit.getText();
input = input.toUpperCase(); //converts the string to uppercase
或强>
在xml中设置: - android:inputType="textCapCharacters"
答案 4 :(得分:1)
使用它可以工作。
android:inputType="textCapSentences"
在你的情况下
<EditText
android:id="@+id/edGymName"
style="@style/LoginRegisterEditText"
android:layout_marginTop="@dimen/size_10"
android:layout_toLeftOf="@+id/txtStatusGymStatus"
android:hint="@string/gym_tag"
android:inputType="textCapSentences"
android:maxLength="30" />
答案 5 :(得分:0)
这将强行从您的editText
重新输入您的整个文字/句子,并将首字母写成大写:
String oldText = ""; //this must be outside the method where the addTextChangedListener on the editText is set. (Preferrably outside the onCreate())
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
if (editable.toString().length() > 0 &&
!editable.toString().equals(oldText)) {
oldText = editable.toString(); //prevent infinite loop
editText.setText(capitalizeFirstLetterWord(editable.toString()));
editText.setSelection(editText.getText().length()); //set the cursor to the end of the editText
}
}
});
方法调用:(我已对其进行了一些修改,请参阅链接)
/**
* reference: http://www.penguincoders.net/2015/06/program-to-capitalize-first-letter-of-each-word-in-java.html
*
* @param s sentence to be capitalize each first letter of each word
* @return capitalized sentence
*/
public static String capitalizeFirstLetterWord(String s) {
StringBuilder cap = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
try {
char x = s.charAt(i);
if (x == ' ') {
cap.append(" ");
char y = s.charAt(i + 1);
cap.append(Character.toUpperCase(y));
i++;
} else {
cap.append(x);
}
} catch (IndexOutOfBoundsException ignored) {
}
}
//finally, capitalize the first letter of the sentence
String sentence = cap.toString();
if (sentence.length() > 0) {
sentence = String.valueOf(sentence.charAt(0)).toUpperCase(); //capitalize first letter
if (cap.toString().length() > 1) { //check if there's succeeding letters
sentence += cap.toString().substring(1); //append it also
}
}
return sentence;
}