【问题标题】:EditText moving long text to next lineEditText 将长文本移动到下一行
【发布时间】:2013-08-21 11:21:05
【问题描述】:

我的应用程序中有一个EditText,我希望它有两行,显示 ime 按钮而不是输入键,并将太长的文本移动到下一行(如在短信应用程序中)。现在我有这样的东西:

<AutoCompleteTextView
    android:id="@+id/name_field"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginLeft="10dp"
    android:layout_weight="1"
    android:background="@null"
    android:freezesText="true"
    android:hint="@string/some_hint"
    android:imeOptions="actionNext"
    android:maxLength="100"
    android:nextFocusDown="@null"
    android:lines="2"
    android:ellipsize="end"
    android:inputType="textImeMultiLine"
    android:selectAllOnFocus="true"
    android:textColor="@android:color/black"
    android:textSize="16sp" />

它有两个我提到的第一个属性,但我不记得有任何选项可以让我达到第三个。

例如:如果我的EditText 中的一行有 10 个字符,我想像这样显示文本“abc abcd abc abcdefghijk”:

abc abcd
abc abc...

编辑: 似乎问题出在android:inputType="textImeMultiLine"。当我将其更改为android:inputType="textMultiLine" 时,一切正常,但是...我有输入按钮而不是 IME 按钮,这是我想避免的。

【问题讨论】:

  • 你能更清楚你到底想要什么..
  • 如果文本不能在一行中显示(因为文本太长),我想在第二行显示其余部分。
  • android:lines 可能是..
  • 如果你给 wrap_content 作为编辑文本的高度,那么它会根据内容的长度自动调整它的高度..
  • 在我的帖子中你可以看到我已经设置了android:lines="2"。不幸的是,没有回车的文本只显示在第一行。

标签: android android-edittext


【解决方案1】:

试试这个..希望它会工作!

<AutoCompleteTextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginLeft="10dp"
    android:background="@null"
    android:freezesText="true"
    android:imeOptions="actionNext"
    android:maxLength="100"
    android:nextFocusDown="@null"
    android:lines="2"
    android:ellipsize="end"
    android:selectAllOnFocus="true"
    android:textColor="@android:color/black"
    android:textSize="16sp" />

【讨论】:

  • 原版和这个有什么区别?
  • 可能缺少android:inputType
【解决方案2】:

添加这一行

android:inputType="textMultiLine"

【讨论】:

    【解决方案3】:

    将此添加到您的 xml 中 android:maxLines="2" 而不是 android:lines="2"

    【讨论】:

      【解决方案4】:

      这些方法都不适合我。 谷歌搜索了大约一个小时后,我找到了这段代码:

      EditText editText = findViewById(R.id.editNote);
          editText.setRawInputType(InputType.TYPE_CLASS_TEXT);
          editText.setImeActionLabel("DONE",EditorInfo.IME_ACTION_DONE);              //Set Return Carriage as "DONE"
          editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
      
          editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
              @Override
              public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
              {
                  if (event == null) {
                      if (actionId == EditorInfo.IME_ACTION_DONE) {
                          // Capture soft enters in a singleLine EditText that is the last EditText
                          // This one is useful for the new list case, when there are no existing ListItems
                          editText.clearFocus();
                          InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(Activity.INPUT_METHOD_SERVICE);
                          inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
                      }
      
                      else if (actionId == EditorInfo.IME_ACTION_NEXT) {
                          // Capture soft enters in other singleLine EditTexts
                      } else if (actionId == EditorInfo.IME_ACTION_GO) {
                      } else {
                          // Let the system handle all other null KeyEvents
                          return false;
                      }
                  }
                  else if (actionId == EditorInfo.IME_NULL) {
                      // Capture most soft enters in multi-line EditTexts and all hard enters;
                      // They supply a zero actionId and a valid keyEvent rather than
                      // a non-zero actionId and a null event like the previous cases.
                      if (event.getAction() == KeyEvent.ACTION_DOWN) {
                          // We capture the event when the key is first pressed.
                      } else {
                          // We consume the event when the key is released.
                          return true;
                      }
                  }
                  else {
                      // We let the system handle it when the listener is triggered by something that
                      // wasn't an enter.
                      return false;
                  }
                  return true;
              }
          });
      

      保持你的 EditText 像这样:

      <EditText
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:id="@+id/editNote"
                  android:hint="Start Typing..."
                  android:inputType="textMultiLine"
                  android:gravity="start" />
      

      我不记得我从哪里得到这个代码,所以不能相信作者。 我根据需要进行了必要的更改。

      【讨论】:

        猜你喜欢
        • 2017-03-02
        • 2012-03-04
        • 2020-12-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-31
        • 2015-11-28
        相关资源
        最近更新 更多