EditText moving long text to next line

2020-07-27 05:51发布

问题:

I have an EditText in my app and I want it to have two lines, to show ime button instead of enter key and to move too long text to next line (like in sms apps). For now i have something like this:

<AutoCompleteTextView
    android:id="@+id/name_field"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginLeft="10dp"
    android:layout_weight="1"
    android:background="@null"
    android:freezesText="true"
    android:hint="@string/some_hint"
    android:imeOptions="actionNext"
    android:maxLength="100"
    android:nextFocusDown="@null"
    android:lines="2"
    android:ellipsize="end"
    android:inputType="textImeMultiLine"
    android:selectAllOnFocus="true"
    android:textColor="@android:color/black"
    android:textSize="16sp" />

It has two first properties I mentioned, but i can't recall any option that allows me to reach third.

For example: if one line in my EditText has 10 chars, i want to display text "abc abcd abc abcdefghijk" like that:

abc abcd
abc abc...

EDIT: It seems problem is in android:inputType="textImeMultiLine". When i changed it to android:inputType="textMultiLine" all works fine, but... I have enter button instead of IME button, which i want to avoid.

回答1:

Try this..Hope it will work!

<AutoCompleteTextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginLeft="10dp"
    android:background="@null"
    android:freezesText="true"
    android:imeOptions="actionNext"
    android:maxLength="100"
    android:nextFocusDown="@null"
    android:lines="2"
    android:ellipsize="end"
    android:selectAllOnFocus="true"
    android:textColor="@android:color/black"
    android:textSize="16sp" />


回答2:

add this in your xml android:maxLines="2" instead of android:lines="2"



回答3:

add this line

android:inputType="textMultiLine"



回答4:

None of these Methods worked for me. After Googling for about an hour, I found this piece of code:

EditText editText = findViewById(R.id.editNote);
    editText.setRawInputType(InputType.TYPE_CLASS_TEXT);
    editText.setImeActionLabel("DONE",EditorInfo.IME_ACTION_DONE);              //Set Return Carriage as "DONE"
    editText.setImeOptions(EditorInfo.IME_ACTION_DONE);

    editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
        {
            if (event == null) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    // Capture soft enters in a singleLine EditText that is the last EditText
                    // This one is useful for the new list case, when there are no existing ListItems
                    editText.clearFocus();
                    InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(Activity.INPUT_METHOD_SERVICE);
                    inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
                }

                else if (actionId == EditorInfo.IME_ACTION_NEXT) {
                    // Capture soft enters in other singleLine EditTexts
                } else if (actionId == EditorInfo.IME_ACTION_GO) {
                } else {
                    // Let the system handle all other null KeyEvents
                    return false;
                }
            }
            else if (actionId == EditorInfo.IME_NULL) {
                // Capture most soft enters in multi-line EditTexts and all hard enters;
                // They supply a zero actionId and a valid keyEvent rather than
                // a non-zero actionId and a null event like the previous cases.
                if (event.getAction() == KeyEvent.ACTION_DOWN) {
                    // We capture the event when the key is first pressed.
                } else {
                    // We consume the event when the key is released.
                    return true;
                }
            }
            else {
                // We let the system handle it when the listener is triggered by something that
                // wasn't an enter.
                return false;
            }
            return true;
        }
    });

Keep your EditText like this:

<EditText
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/editNote"
            android:hint="Start Typing..."
            android:inputType="textMultiLine"
            android:gravity="start" />

I don't remember where I got this code from so can't credit the author. I made the necessary changes according to my need.