【问题标题】:Android EditText: Done instead of Enter or Word Wrap instead of Multi LineAndroid EditText:完成而不是 Enter 或 Word Wrap 而不是 Multi Line
【发布时间】:2011-07-07 15:57:41
【问题描述】:

我有一个不允许换行的多行 EditText。现在,只要他们点击保存,我就会用一些空格替换退货。有什么办法可以用完成按钮替换屏幕上的输入按钮? (就像单行 EditText 一样)

我知道我仍然应该去掉返回 (\r\n|\r|\n),因为屏幕键盘不是添加它们的唯一方法。

这是我当前的 XML

<EditText android:layout_width="fill_parent" android:layout_height="wrap_content"
          android:minLines="3" android:gravity="left|top"
          android:inputType="textMultiLine|textAutoCorrect|textCapSentences"
          android:imeOptions="actionDone" />

【问题讨论】:

标签: android action android-edittext word-wrap multiline


【解决方案1】:
android:inputType="textEmailAddress|textEmailSubject"

您需要将输入类型设置为电子邮件地址或电子邮件主题。任何一个都会给你你想要的结果。 shouldAdvanceFocusOnEnter() 是 TextView 中的一个私有方法,它决定是进入新行还是将焦点移动到下一个字段。

【讨论】:

  • 这真的适用于多行文本吗?如果我不在 textMultiLine 中使用管道,我只能获得完成按钮。
【解决方案2】:

我建议阅读这篇文章

http://savagelook.com/blog/android/android-quick-tip-edittext-with-done-button-that-closes-the-keyboard

很好的例子

XML:

<EditText 
    android:id="@+id/edittext_done"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="Enter some text"
    android:imeOptions="actionDone"
    />

自定义动作类:

class DoneOnEditorActionListener implements OnEditorActionListener {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
            return true;  
        }
        return false;
    }
}

活动类:

public class SampleActivity extends Activity {    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sample_activity_layout); // sample_activity_layout contains our target EditText, target_edittext
 
        EditText targetEditText = (EditText)findViewById(R.id.target_edittext); 
        targetEditText.setOnEditorActionListener(new DoneOnEditorActionListener());
 
        // The rest of the onCreate() code
   }
}

【讨论】:

  • 由于不鼓励仅链接的答案,我继续并在相关代码中进行了编辑。谢谢!
  • @GeorgeBailey 为什么当您默认使用单行自动获得“下一个”或“完成”功能时,该示例专注于单行编辑文本。据我所知,一旦您输入 inputType textMultiLine,设置 imeOption actionDone 不会将您的回车键变成完成按钮。
  • @towpse,我不知道。我没有使用这个解决方案。也许您可以提出一个问题并从知道的人那里得到更好的答案。如果我猜的话,我会说 imeOption actionDone 仅在您将其与 Java 类源代码的两个部分正确结合时才有效。或者您应该阅读链接的文章以获得进一步的解释。
  • @GeorgeBailey 您是否成功地修改了多行文本框的输入/操作键?如果我不在 textMultiLine 中使用管道,我只能获得完成按钮。
  • @towpse,我不记得了。已经6个月了。我建议您提出一个新问题,包括您尝试过的示例代码、当前结果以及您想要的结果的描述。
【解决方案3】:

如果您在 XML 中使用 android:inputType="textMultiLine|...",或使用相应的 Java 代码:

editField.setInputType(
    InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE);

那么显示 ✔︎ 完成? 搜索 按钮的唯一解决方案是按照此处的答案进行操作:

Multiline EditText with Done SoftInput Action Label on 2.3

所以您应该扩展EditText 并覆盖onCreateInputConnection() 以手动设置IME_ACTION_xx 标志;像这样的……

@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
    InputConnection connection = super.onCreateInputConnection(outAttrs);
    int imeActions = outAttrs.imeOptions&EditorInfo.IME_MASK_ACTION;
    if ((imeActions & EditorInfo.IME_ACTION_DONE) != 0) {
        // clear the existing action
        outAttrs.imeOptions ^= imeActions;
        // set the DONE action
        outAttrs.imeOptions |= EditorInfo.IME_ACTION_DONE;
    }
    if ((outAttrs.imeOptions & EditorInfo.IME_FLAG_NO_ENTER_ACTION) != 0) {
        outAttrs.imeOptions &= ~EditorInfo.IME_FLAG_NO_ENTER_ACTION;
    }
    return connection;
}

这是因为每当您启用"textMultiLine" 选项时,它都会忽略android:imeOptions="actionDone"android:imeActionLabel="actionDone" 的任何设置,这非常奇怪和令人困惑。

【讨论】:

    【解决方案4】:

    我对带有 actionLabel 的多行文本执行此操作:

    editText.setSingleLine(true);
    editText.setLines(10);
    editText.setHorizontallyScrolling(false);
    editText.setImeActionLabel(getString(R.string.ready), 0);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-17
      • 1970-01-01
      • 2023-03-26
      • 2012-07-05
      • 1970-01-01
      • 1970-01-01
      • 2020-08-27
      相关资源
      最近更新 更多