2

我的应用程序中有一个EditText,我希望它有两行,显示 ime 按钮而不是输入键,并将太长的文本移动到下一行(如在短信应用程序中)。现在我有这样的事情:

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

它有两个我提到的第一个属性,但我不记得有任何选项可以让我达到第三个。

例如:如果我的一行EditText有 10 个字符,我想像这样显示文本“abc abcd abc abcdefghijk”:

abc abcd
abc abc...

编辑:似乎问题出在android:inputType="textImeMultiLine". 当我将其更改为android:inputType="textMultiLine"一切正常时,但是...我有输入按钮而不是 IME 按钮,这是我想避免的。

4

4 回答 4

1

试试这个..希望它会奏效!

<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" />
于 2013-08-21T12:05:58.487 回答
1

add this line

android:inputType="textMultiLine"

于 2017-01-21T13:55:37.933 回答
0

将此添加到您的 xmlandroid:maxLines="2"中,而不是android:lines="2"

于 2013-08-21T11:49:48.497 回答
0

这些方法都不适合我。谷歌搜索了大约一个小时后,我找到了这段代码:

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

保持你的 EditText 像这样:

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

我不记得我从哪里得到这个代码,所以不能相信作者。我根据需要进行了必要的更改。

于 2018-09-25T17:28:24.193 回答