【问题标题】:Set edittext increase upto 3 lines while typing in android在android中输入时设置edittext最多增加3行
【发布时间】:2016-01-05 10:48:55
【问题描述】:

我想将我的 editText 设置为仅在我输入时最多增加 3 行。我尝试了 minlines 但它甚至在用户输入任何内容之前就显示了 3 行编辑文本。我什至尝试了 wrap_content 但没有任何效果。请问有人可以帮忙吗?

 <LinearLayout
            android:id="@+id/ln"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:orientation="horizontal"
            android:visibility="visible"
            android:background="#5f6799"
            >


            <EditText
                android:id="@+id/edComments"
                android:layout_width="120dp"
                android:layout_height="match_parent"
                android:layout_marginTop="6dp"
                android:layout_marginBottom="6dp"
                android:layout_weight="0.95"
                android:layout_marginLeft="11dp"
                android:layout_marginRight="8dp"
                 android:scrollbars="vertical"
                android:paddingLeft="5dp"
                android:maxLength="140"
                android:inputType="textMultiLine|textCapSentences"
                android:imeOptions="actionDone"
                android:ems="10"
                android:hint="Write your comment here"
                android:textColor="@color/my_dark_gray"
                android:background="@color/white"
                android:textSize="13sp"
                />

        </LinearLayout>

【问题讨论】:

  • 尝试添加 textChanged 监听器,然后在 OnTextChanged 方法中将 Minlines 设置为您想要的

标签: android android-edittext


【解决方案1】:

android:layout_height="wrap_content" 设置android:maxLines="3" 应该可以解决问题。

【讨论】:

    【解决方案2】:

    试试这个方法就行了

     android:minLines="1"
       android:maxLines="3"
    

    【讨论】:

      【解决方案3】:

      将此添加到您的EditText 中,高度为wrap_content

      <EditText
         android:id="@+id/edComments"
          android:layout_height="wrap_content"
         android:minLines="1"
         android:maxLines="3"
      

      【讨论】:

        【解决方案4】:

        谢谢你们。 Vivek Mishra 的解决方案对我有用。

        edComments.addTextChangedListener(watcher);
        
        TextWatcher watcher = new TextWatcher(){
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        
                }
        
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    edComments.setMinLines(3);
                }
        
                @Override
                public void afterTextChanged(Editable s) {
        
                }
            };
        

        【讨论】:

          【解决方案5】:
          <EditText
                      android:id="@+id/edComments"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:gravity="top"
                      android:hint="Write your comment here"
                      android:inputType="textMultiLine"
                      android:minLines="1"
                      android:maxLines="3"
                      android:maxLength="140"
                      android:paddingBottom="10dp"
                      android:paddingLeft="10dp"
                      android:paddingRight="10dp"
                      android:paddingTop="10dp" />
          

          【讨论】:

            猜你喜欢
            • 2012-04-04
            • 1970-01-01
            • 2013-07-30
            • 2015-10-18
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-11-05
            • 1970-01-01
            相关资源
            最近更新 更多