EditText包含单行文本,换行和完成操作?

时间:2022-10-17 22:17:22

I am trying to have an EditText with the following characteristics when editing with a soft key. I ready the documentation, searched here, play with the parameters but could not find a working configuration.

我正在尝试使用软键编辑时具有以下特征的EditText。我准备了文档,在这里搜索,玩参数但找不到工作配置。

  1. The EditView view on the screen has height for a few lines (e.g. 3-4).
  2. 屏幕上的EditView视图具有几行高度(例如3-4)。

  3. The content text is a single line (that is, no line breaks).
  4. 内容文本是单行(即没有换行符)。

  5. If the content text is longer than the view's width it should wrap to next line
  6. 如果内容文本长于视图的宽度,则应该换行到下一行

  7. The Enter key of the soft key shows the Done action label.
  8. 软键的Enter键显示完成操作标签。

I could achieve {1,2,3} and {1,2,4} but not {1,2,3,4}. My rational is that since the content is a single line (no line breaks) the Enter key is not used and thus should be able to be changed to the Done label.

我可以实现{1,2,3}和{1,2,4}但不能实现{1,2,3,4}。我的理由是,由于内容是单行(没有换行符),因此不使用Enter键,因此应该能够更改为Done标签。

My setup code looks like this

我的设置代码如下所示

editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES | InputType.TYPE_TEXT_FLAG_IME_MULTI_LINE);
editText.setHorizontallyScrolling(false);
editText.setSingleLine(false);
// This does not work. Soft keyboard has Enter action.
editText.setImeOptions(EditorInfo.IME_ACTION_DONE);

Is it possible? Any suggestion?

可能吗?有什么建议吗?

2 个解决方案

#1


33  

This combination (and the specific order of the EditText method calls) should produce the configuration that you want:

这种组合(以及EditText方法调用的特定顺序)应该产生您想要的配置:

  editText.setInputType(
    InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);
  editText.setSingleLine(true);
  editText.setLines(4); // desired number of lines
  editText.setHorizontallyScrolling(false);
  editText.setImeOptions(EditorInfo.IME_ACTION_DONE);

#2


7  

Just add

editText.setHorizontallyScrolling(false);
editText.setMaxLines(Integer.MAX_VALUE);

with your edittext instance in your activity programmatically.

以编程方式在您的活动中使用您的edittext实例。

It configures the EditText instance so that the user edits a single-line string that is displayed with soft-wrapping on multiple lines with IME options.

它配置EditText实例,以便用户编辑单行字符串,该字符串使用IME选项在多行上进行软包装。

#1


33  

This combination (and the specific order of the EditText method calls) should produce the configuration that you want:

这种组合(以及EditText方法调用的特定顺序)应该产生您想要的配置:

  editText.setInputType(
    InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);
  editText.setSingleLine(true);
  editText.setLines(4); // desired number of lines
  editText.setHorizontallyScrolling(false);
  editText.setImeOptions(EditorInfo.IME_ACTION_DONE);

#2


7  

Just add

editText.setHorizontallyScrolling(false);
editText.setMaxLines(Integer.MAX_VALUE);

with your edittext instance in your activity programmatically.

以编程方式在您的活动中使用您的edittext实例。

It configures the EditText instance so that the user edits a single-line string that is displayed with soft-wrapping on multiple lines with IME options.

它配置EditText实例,以便用户编辑单行字符串,该字符串使用IME选项在多行上进行软包装。