强制制作每个单词都以Editext中的Capital开头 - 用户不应该更改它
我有一个要求,即Edittext应该具有以大写字母开头的所有单词。如果用户以较小的大小写(单词的第一个字母),那么它也应该被转换成大写。强制制作每个单词都以Editext中的Capital开头 - 用户不应该更改它
我已经做到了布局如下至今:
<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" /> 
但是,我不想让用户写的字的第一个字母的小写字母。这是行得通的,但用户可以在小案例中写出单词的第一个字母。如果我们强行不允许的话会怎么样?
回答:
试试这个在XML文件中:
android:inputType="text|textCapCharacters" 你可以做的另一件事情是这样的,它将用户更改后重写你的编辑文本。
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 
     } 
    } 
}); 
以上是 强制制作每个单词都以Editext中的Capital开头 - 用户不应该更改它 的全部内容, 来源链接: utcz.com/qa/263500.html


