【问题标题】:EditText automatically go to a new lineEditText 自动换行
【发布时间】:2014-06-01 04:12:36
【问题描述】:

我想让EditText 的最大行数为 2,最大长度为 20。如果 EditText 的长度超过 10,它应该自动转到新行,这样用户就不需要按进入。有人可以帮我解决这个要求吗?

【问题讨论】:

  • 我没有尝试过,但我认为您应该尝试使用 TextWatcher。当长度达到 10 时,在当前 String 中添加“\n”。

标签: android android-edittext


【解决方案1】:

textView.setText("你的文字"+"\n"+"你的文字");

【讨论】:

    【解决方案2】:

    将此添加到您的 EditText xml 代码中

    android:inputType="textMultiLine"

    这将在输入数据时自动将您的文本移动到下一行。

    【讨论】:

    • 这不会在字符串中添加 \n 字符。只是在 EditText 视图中换行,这不是 OP 所要求的。
    【解决方案3】:

    它应该以最优雅的方式存在,但此解决方案可能会帮助您作为实现所需目标的线索。首先,您需要将EditText 值设置如下:

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="text|textMultiLine|textCapSentences"
        android:maxLength="21"
        android:gravity="left"
        android:maxLines="2" />  
    

    您必须将maxLength 属性设置为21,因为输入字符(新行)将在编辑文本中占用一个字符,然后用户将只能写19 字符而不是20
    然后,您应该使用 TextWatcherboolean(如果用户删除他以前的字符时使用),这应该如下:

    // init global boolean
    private boolean isReached = false;
    
    // in onCreate method
    edittext.addTextChangedListener(new TextWatcher(){
        @Override
        public void afterTextChanged(Editable s) {
            // if edittext has 10chars & this is not called yet, add new line
            if(textEd.getText().length() == 10 && !isReached) {
                textEd.append("\n");
                isReached = true;
            }
            // if edittext has less than 10chars & boolean has changed, reset
            if(textEd.getText().length() < 10 && isReached) isReached = false;
        }
    });  
    

    注意:但是,您应该小心使用此代码。实际上,用户仍然可以按下Key Enter,然后添加新行。也许这些答案可能会帮助您处理它并让用户只“在路上”:Prevent enter key on EditText but still show the text as multi-line

    【讨论】:

    • 对于第二行和后续行,代码将不会处理移动到下一行。它导致进行数学计算
    【解决方案4】:

    让 EditText 显示 2 行。你可以这样写:

    android:maxLength="20"
    android:maxLines="2"
    android:singleLine="false"
    android:layout_width="Your Choice"
    

    【讨论】:

      【解决方案5】:

      对于线条,请执行以下操作:

      <EditText
          android:inputType="textMultiLine" <!-- Multiline input -->
          android:lines="2" <!-- Total Lines prior display -->
          android:minLines="1" <!-- Minimum lines -->
          android:gravity="top|left" <!-- Cursor Position -->
          android:maxLines="2" <!-- Maximum Lines -->
          android:layout_height="wrap_content"
          android:layout_width="fill_parent"/>
      

      您也可以使用此参数(不适用于 Android ):

      android:singleLine="false"
      

      对于字符数,请执行以下操作:

      EditText et= (EditText) findViewById(R.id.editText1);
      InputFilter[] filters = new InputFilter[1];
      filters[0] = new InputFilter.LengthFilter(20); //Filter to 20 characters
      et.setFilters(filters);
      

      或者,使用这个其他参数,

      android:maxLength="20" 中的xml

      【讨论】:

      • 谢谢你的回答,但我想在edittext长度超过10时自动输入,所以基本上每行10个字符,maxline只有2个
      • 仅供参考 maxLines 属性(当设置 > 2 行时)在 Android
      猜你喜欢
      • 2015-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-03
      • 1970-01-01
      • 1970-01-01
      • 2013-07-30
      相关资源
      最近更新 更多