android实现EditText的多行输入框

作者: 奔跑的佩恩 | 来源:发表于2017-07-01 16:18 被阅读530次

    项目开发的过程中,有时会用到类似写评论的事,这就涉及到EditText的多行输入了,涉及到的属性有:

    android:minLines="5"  
    android:gravity="top"
    

    这里解释下:minLines是只输入的最小行数,gravity="top"是为了让文字从输入框上面依次排版下来,而不是显示在输入框中间。

    在这里,让我联想到 另一个属性:maxLines

    简单讲下 minLines和 maxLines区别吧:

    使用maxLines的EditText最大行数为3行,当输入的内容超过3行后,
    它形状的大小不会根据输入内容的多少而改变,
    反正它显示的内容就是3行
    而使用minLines的EditText是至它至少显示3行内容
    (包括内容为空时,可对比图片),当输入的内容超过3行后,
    它形状的大小根据输入内容的多少而改变。
    

    然后看看我对 EditText多行输入的xml设置吧

    <EditText
                            android:id="@+id/edt_order_note_text"
                            android:layout_width="match_parent"
                            android:layout_height="@dimen/dp_90"
                            android:padding="@dimen/dp_5"
                            android:minLines="5"
                            android:gravity="top"
                            android:background="@drawable/order_edt_note_bg"
                            android:textColor="@color/black"
                            android:textSize="@dimen/sp_14"/>
    

    @drawable/order_edt_note_bg是一个输入框背景的xml,order_edt_note_bg.xml代码如下:

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <corners android:radius="@dimen/dp_6"></corners>
        <stroke android:color="@color/color_c0c0c0"
            android:width="@dimen/dp_1"></stroke>
    </shape>
    

    效果图大致就下面这个样子吧

    1.png

    今天的内容比较简单,就到这里吧,谢谢诶。

    相关文章

      网友评论

        本文标题:android实现EditText的多行输入框

        本文链接:https://www.haomeiwen.com/subject/uhfgcxtx.html