如何在Android中完成输入后将多行edittext限制为单行?

问题描述 投票:0回答:1

我正在使用多行EditText。键入完成并触摸另一视图后,我希望editText仅显示一行。怎么做?

<EditText
    android:id="@+id/descriptionfield"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textMultiLine"
    />
android-studio android-layout android-edittext android-xml
1个回答
0
投票

您可以将setOnFocusChangeListener设置为如下所示的编辑文本

 descriptionfield.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    descriptionfield.setSingleLine(false);
                } else {
                    descriptionfield.setSingleLine(true);
                }
            }
        });

如果edittext具有焦点,则setSingleLinefalse否则setSingleLinetrue

希望对您有帮助

© www.soinside.com 2019 - 2024. All rights reserved.