First letter capitalization for EditText

后端 未结 17 1834
傲寒
傲寒 2020-11-28 01:11

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

相关标签:
17条回答
  • 2020-11-28 01:47

    Earlier it used to be android:capitalize="words", which is now deprecated. The recommended alternative is to use android:inputType="textCapWords"

    Please note that this will only work if your device keyboard Auto Capitalize Setting enabled.

    To do this programatically, use the following method:

    setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_WORDS);

    0 讨论(0)
  • 2020-11-28 01:49

    Programmatically

    TextView firstline = (TextView) findViewById(R.id.firstline);
    firstline.setAllCaps(true);
    
    0 讨论(0)
  • 2020-11-28 01:51

    Set input type in XML as well as in JAVA file like this,

    In XML,

    android:inputType="textMultiLine|textCapSentences"

    It will also allow multiline and in JAVA file,

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

    make sure your keyboard's Auto-Capitalization setting is Enabled.

    0 讨论(0)
  • 2020-11-28 01:56

    Just use android:inputType="textCapWords" in your EditText element.

    For example:

    <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" />
    

    Refer to the following link for reference: http://developer.android.com/reference/android/widget/TextView.html#attr_android%3ainputType

    0 讨论(0)
  • 2020-11-28 01:56

    i founded and my solution : you have 2 way to resolve it in java :

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

    and xml :

     <EditText
    android:id="@+id/mytxt"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textCapWords"
    android:textSize="12sp" />
    
    0 讨论(0)
  • 2020-11-28 01:57

    I can assure you both the answers will make first letter capital and will not make edittext single line.

    If you want to do it in XMl below is the code

    android:inputType="textCapWords|textCapSentences"
    

    If want to do it in activity/fragment etc below is the code

    momentTextView.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_WORDS | InputType.TYPE_TEXT_FLAG_MULTI_LINE)
    

    PS: If you are having nay other property also you can easily add it with a pipe "|" symbol, just make sure there is no space in xml between the attribute properties

    0 讨论(0)
提交回复
热议问题