Android EditText:使用“完成”替代“回车”,或者使用“自动换行”替代“多行”。

15

我有一个多行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" />

https://dev59.com/PW445IYBdhLWcg3wJ298#5037488 - AZ_
4个回答

26

我建议阅读这篇文章

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;
    }
}

Activity类:

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
   }
}

1
由于仅提供链接的答案不被鼓励,因此我已经编辑了相关代码。谢谢! - 700 Software
@towpse,我不知道。我没有使用过这个解决方案。也许你可以提出一个问题,从那些了解的人那里得到更好的答案。如果我要猜的话,我会说imeOption actionDone只有在正确结合Java类源代码的两个部分时才能起作用。或者你应该阅读链接的文章以获得进一步的解释。 - 700 Software
@towpse,我不记得了。已经过去6个月了。我建议你提出一个新问题,附上你尝试过的示例代码、当前结果以及所需结果的描述。 - 700 Software
@GeorgeBailey 哎呀,我刚刚编写了一个代码,让回车键隐藏了键盘并像完成按钮一样工作,现在这可以使用了,谢谢。 - topwik
那个链接指向一个已禁用的网站。 - Azurespot
显示剩余2条评论

20
android:inputType="textEmailAddress|textEmailSubject"

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


3
这对于多行文本是否有效?如果我不将文本框设置为多行文本,我才能获得“完成”按钮。 - topwik

5

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

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

如果想要展示一个 ✔︎ Done 或者 Search 按钮,唯一的解决方案就是按照这里的答案操作: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);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接