ChatGPT解决这个技术问题 Extra ChatGPT

First letter capitalization for EditText

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.


C
Cactus

Statically (i.e. in your layout XML file): set android:inputType="textCapSentences" on your EditText.

Programmatically: you have to include InputType.TYPE_CLASS_TEXT in the InputType of the EditText, e.g.

EditText editor = new EditText(this); 
editor.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);

Can be combined with text and its variations to request capitalization of the first character of every sentence.

- Google Docs


testEditText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_WORDS); or android:inputType="textCapSentences" will only work If your device keyboard Auto Capitalize Setting enabled.
Why does enabling this cause my android:lines="7" to no longer show 7 lines (it now defaults back to 1 line)
estEditText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_WORDS); or android:inputType="textCapSentences" will only work If your device keyboard Auto Capitalize Setting enabled. What is the alternative for this, programmtically ?
@JamesWierzba I will refer here Shivang answer as response for multi-line support. Add android:inputType="textCapSentences|textMultiLine"
Use android:inputType="text|textMultiLine|textCapSentences" to allow multiple lines text
R
Ravi Bhatt

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


It is good practice to mark the code in code element instead make it bold.
This answer doesn't really help OP's question. This will capitalize the first character for each word, not the first character of the input text (which is assumed as a sentence). While this may probably help the others, the correct answer is the accepted answer.
A
Akhilesh Dhar Dubey

Apply following line in your EditText in XML.

android:inputType="textCapSentences|textMultiLine"

It will also allow multi-line support.


That should be an accepted answer, as this is the only solution which won't collapse EditText into single-line
A
Adil Soomro
testEditText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_WORDS);   

or android:inputType="textCapSentences" will only work If your device keyboard Auto Capitalize Setting enabled.


Thanks, I was stuck because I just disabled device keyboard Auto Capitalize Setting
Z
Ziem

I encountered the same problem, just sharing what I found out. Might help you and others...

Try this on your layout.add the line below in your EditText.

android:inputType="textCapWords|textCapSentences"

works fine on me.. hope it works also on you...


S
Suraj Vaishnav

To capitalize, you can do the following with edit text:

To make first letter capital of every word:

android:inputType="textCapWords"

To make first letter capital of every sentence:

android:inputType="textCapSentences"

To make every letter capital:

android:inputType="textCapCharacters"

But this will only make changes to keyboard and user can change the mode to write letter in small case.

So this approach is not much appreciated if you really want the data in capitalize format, add following class first:

public class CapitalizeFirstLetter {
    public static String capitaliseName(String name) {
        String collect[] = name.split(" ");
        String returnName = "";
        for (int i = 0; i < collect.length; i++) {
            collect[i] = collect[i].trim().toLowerCase();
            if (collect[i].isEmpty() == false) {
                returnName = returnName + collect[i].substring(0, 1).toUpperCase() + collect[i].substring(1) + " ";
            }
        }
        return returnName.trim();
    }
    public static String capitaliseOnlyFirstLetter(String data)
    {
        return data.substring(0,1).toUpperCase()+data.substring(1);
    }
}

And then,

Now to capitalize every word:

CapitalizeFirstLetter.capitaliseName(name);

To capitalize only first word:

CapitalizeFirstLetter.capitaliseOnlyFirstLetter(data);

J
Jewel Rana

If you want capital first letter in every word then use android:inputType="textCapWords"

For Better understanding

 <EditText
    android:id="@+id/edt_description"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textCapWords"/>

And if you capital word for every sentence then use it . android:inputType="textCapSentences" this line in your xml. I mean in your EditText.

For Better understanding

<EditText
    android:id="@+id/edt_description"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textCapSentences"/>
    

A
Anand Tripathi

For Capitalisation in EditText you can choose the below two input types:

android:inputType="textCapSentences" android:inputType="textCapWords"

textCapSentences This will let the first letter of the first word as Capital in every sentence.

textCapWords This will let the first letter of every word as Capital.

If you want both the attributes just use | sign with both the attributes

android:inputType="textCapSentences|textCapWords"

T
Tushar Srivastava

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


A
Agilanbu

In your layout XML file :

set android:inputType="textCapSentences"

On your EditText to have first alphabet of the first word of each sentence as capital

Or android:inputType="textCapWords" on your EditText to have first alphabet of each word as capital


S
Siddharth Sheth

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.


D
Dharmesh Patel

Try This Code, it will capitalize first character of all words.

- set addTextChangedListener for EditText view

edt_text.addTextChangedListener(watcher);

- Add TextWatcher

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);
        }
    }
};

m
milan pithadia

use this code to only First letter capitalization for EditText

MainActivity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:id="@+id/et"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:tag="true">
    </EditText>

</RelativeLayout>

MainActivity.java

EditText et = findViewById(R.id.et);
        et.addTextChangedListener(new TextWatcher() {
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2)
            {
                if (et.getText().toString().length() == 1 && et.getTag().toString().equals("true"))
                {
                    et.setTag("false");
                    et.setText(et.getText().toString().toUpperCase());
                    et.setSelection(et.getText().toString().length());
                }
                if(et.getText().toString().length() == 0)
                {
                    et.setTag("true");
                }
            }

            public void afterTextChanged(Editable editable) {

            }
        });

佚名

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

M
Mahendra Liya

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);


S
Suraj Rao

Programmatically

TextView firstline = (TextView) findViewById(R.id.firstline);
firstline.setAllCaps(true);

s
saigopi.me

if you are writing styles in styles.xml then

remove android:inputType property and add below lines

<item name="android:capitalize">words</item>