首页 > 解决方案 > 在Android中输入完成后如何将多行编辑文本限制为单行?

问题描述

我正在使用多行 EditText。输入完成并触摸另一个视图后,我希望 editText 只出现一行。怎么做?

<EditText
    android:id="@+id/descriptionfield"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textMultiLine"
    />

标签: android-studioandroid-layoutandroid-edittextandroid-xml

解决方案


您可以设置setOnFocusChangeListener为您的edittext,如下所示

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

如果edittext有焦点然后setSingleLinefalseelse setSingleLinetotrue

我希望这可以帮助你


推荐阅读