2017-12-18 2 views
-4

私は、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" /> 

しかし、私は、ユーザーが小さな文字で単語の最初の文字を書くことができますする必要はありません。これは動作していますが、ユーザーは小文字に単語の最初の文字を書くことができます。もし私たちが強制的にそれを許さなければどうですか?

+0

@Ankita質問を完全に読み、 "FORCEFULLY"という単語の意味を理解してください。単語ごとに最初の文字を小さくしないでください。 –

+2

TextWatcherを実装すると、afterTextChanged()の最初の文字を大文字にすることができます。 –

答えて

0

あなたの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 
     } 

    } 
}); 
+0

は機能しません。 –

関連する問題