Android EditText with word-wrap but no hard returns

后端 未结 3 1960
小鲜肉
小鲜肉 2020-12-31 11:07

If I set SingleLine=true on an EditText widget, I get a single-line edit control that doesn\'t allow hard returns to be inserted by the user (clicking Enter moves to the nex

相关标签:
3条回答
  • 2020-12-31 11:28
    editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
    //This will treat Enter as Done instead of new line:
    editText.setRawInputType(InputType.TYPE_CLASS_TEXT); 
    
    And in XML:
    android:inputType="textMultiLine"
    
    0 讨论(0)
  • 2020-12-31 11:43

    I was interested in this, and realized that the built-in Android messaging app has wrapped text without the enter key on the soft keyboard. According to the source layout here, it's done with the following options. Not all are necessary; I believe the key is to have textShortMessage, textMultiLine, and flagNoEnterAction:

    android:inputType="textShortMessage|textAutoCorrect|textCapSentences|textMultiLine"
    android:imeOptions="actionSend|flagNoEnterAction"
    
    0 讨论(0)
  • 2020-12-31 11:46

    I too was looking for something to do this as well. The only solution I found was to extend the EditText as follows:

    package com.kylemilligan.test;
    
    import android.content.Context;
    import android.util.AttributeSet;
    import android.view.inputmethod.EditorInfo;
    import android.view.inputmethod.InputConnection;
    import android.widget.EditText;
    
    public class NoNewlineEditText extends EditText
    {
    
        public NoNewlineEditText(Context context) {
            super(context);
        }
    
        public NoNewlineEditText(Context context, AttributeSet attributeSet) {
            super(context, attributeSet);
        }    
    
        @Override
        public InputConnection onCreateInputConnection(EditorInfo outAttrs)
        {
            InputConnection connection = super.onCreateInputConnection(outAttrs);
            int imeActions = outAttrs.imeOptions & EditorInfo.IME_MASK_ACTION;
            if ((imeActions & EditorInfo.IME_ACTION_DONE) != 0)
            {
                // clear the existing action
                outAttrs.imeOptions ^= imeActions;
                // set the DONE action
                outAttrs.imeOptions |= EditorInfo.IME_ACTION_DONE;
            }
            if ((outAttrs.imeOptions & EditorInfo.IME_FLAG_NO_ENTER_ACTION) != 0)
            {
                outAttrs.imeOptions &= ~EditorInfo.IME_FLAG_NO_ENTER_ACTION;
            }
            return connection;
        }
    }
    

    And then in XML use like:

            <com.kylemilligan.test.NoNewlineEditText
                android:id="@+id/noNewLineText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="top|left"
                android:imeOptions="actionDone"
                android:minLines="5" />
    

    Hope this helps!

    0 讨论(0)
自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题