Android EditText:完成而不是 Enter 或自动换行而不是多行

发布于 2024-10-20 04:21:51 字数 480 浏览 6 评论 0原文

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

I have a multiple line EditText that does not permit line returns. Right now I am replacing returns with some spaces as soon as they click save. Is there any way I can replace the on screen enter button with a Done button? (like it is for single line EditText)

I am aware that I should still strip out returns (\r\n|\r|\n) because the on screen keyboard is not the only way to add them.

Here is my current 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" />

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

℉絮湮 2024-10-27 04:21:51

我建议阅读这篇文章

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

非常好的例子

XML:

<前><代码><编辑文本
android:id="@+id/edittext_done"
安卓:layout_width =“fill_parent”
安卓:layout_height =“wrap_content”
android:hint="输入一些文字"
android:imeOptions="actionDone"
>>

自定义操作类:

类 DoneOnEditorActionListener 实现 OnEditorActionListener {
    @覆盖
    公共布尔onEditorAction(TextView v,int actionId,KeyEvent事件){
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
            返回真;  
        }
        返回假;
    }
}

活动类别:

public class SampleActivity 扩展 Activity {    
    @覆盖
    公共无效onCreate(捆绑保存实例状态){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sample_activity_layout); //sample_activity_layout包含我们的目标EditText,target_edittext
 
        EditText targetEditText = (EditText)findViewById(R.id.target_edittext); 
        targetEditText.setOnEditorActionListener(new DoneOnEditorActionListener());
 
        // onCreate() 代码的其余部分
   }
}

I suggest to read this article

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

really good example

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"
    />

Custom Action Class:

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 Class:

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
   }
}
神也荒唐 2024-10-27 04:21:51
android:inputType="textEmailAddress|textEmailSubject"

您需要将输入类型设置为电子邮件地址或电子邮件主题。任何一种都会给您带来您想要的结果。 shouldAdvanceFocusOnEnter() 是 TextView 中的私有方法,用于确定是否输入新行或将焦点移至下一个字段。

android:inputType="textEmailAddress|textEmailSubject"

You need to set the input type as email address or email subject. Either one will give you your desired result. shouldAdvanceFocusOnEnter() is a private method in TextView which determines whether to enter a new line or move focus to next field.

柏林苍穹下 2024-10-27 04:21:51

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

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

那么这是显示 ✔︎ 完成 的唯一解决方案或

If you're using android:inputType="textMultiLine|..." in your XML, or using the corresponding Java code:

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

then the only solution to show a ✔︎ Done or ???? Search button is to follow the answers here:

Multiline EditText with Done SoftInput Action Label on 2.3

So you should extend EditText and override onCreateInputConnection() to manually set the IME_ACTION_xx flags; something like this...

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

This is because whenever you enable the "textMultiLine" option, it ignores any setting of android:imeOptions="actionDone" or android:imeActionLabel="actionDone", which is very strange and confusing.

吾家有女初长成 2024-10-27 04:21:51

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

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

I do this for multiline texts with an actionLabel:

editText.setSingleLine(true);
editText.setLines(10);
editText.setHorizontallyScrolling(false);
editText.setImeActionLabel(getString(R.string.ready), 0);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文