android多行文本框控件,android - 在2.3上使用Done SoftInput动作标签的多行EditText

android - 在2.3上使用Done SoftInput动作标签的多行EditText

有没有办法让多线InputType.TYPE_TEXT_FLAG_MULTI_LINE出现并在Android 2.3上使用IME Action Label“Done”?

在Android 2.2中,这不是问题,输入按钮显示IME操作标签“完成”(InputType.TYPE_TEXT_FLAG_MULTI_LINE),并在单击时取消软输入。

在为多行配置InputType.TYPE_TEXT_FLAG_MULTI_LINE时,Android 2.3无法显示软输入键盘的“完成”操作。

我已设法通过使用InputType.TYPE_TEXT_FLAG_MULTI_LINE更改软输入输入按钮的行为,但是输入按钮仍然看起来像一个回车键。

这是InputType.TYPE_TEXT_FLAG_MULTI_LINE的声明

android:id="@+id/Comment"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_marginTop="10dp"

android:layout_marginBottom="0dp"

android:lines="3"

android:maxLines="3"

android:minLines="3"

android:maxLength="60"

android:scrollHorizontally="false"

android:hint="hint"

android:gravity="top|left"

android:textColor="#888"

android:textSize="14dp"

/>

当我在活动中加载设置内容视图后检查InputType.TYPE_TEXT_FLAG_MULTI_LINE值时,它显示为:

inputType = 0x20001

这是:

class = InputType.TYPE_TEXT_FLAG_MULTI_LINE

flags = InputType.TYPE_TEXT_FLAG_MULTI_LINE

7个解决方案

156 votes

那么,在重新阅读IME_ACTION_DONE和EditorInfo文档之后,很明显该平台将强制IME_ACTION_DONE用于多行文本视图。

请注意,IME_ACTION_DONE将自动生效   在多行上为你设置这个标志   文字观点。

我的解决方案是子类IME_ACTION_DONE并在让平台配置后调整IME选项:

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

}

在上面,我也强迫IME_ACTION_DONE,即使这可以通过繁琐的布局配置来实现。

ohhorob answered 2019-04-11T17:10:50Z

101 votes

Ohhorob的答案基本上是正确的,但他的代码实际上是多余的! 它基本上等同于这个更简单的版本(懒惰读者的完整代码):

package com.example.views;

import android.content.Context;

import android.util.AttributeSet;

import android.view.inputmethod.EditorInfo;

import android.view.inputmethod.InputConnection;

import android.widget.EditText;

// An EditText that lets you use actions ("Done", "Go", etc.) on multi-line edits.

public class ActionEditText extends EditText

{

public ActionEditText(Context context)

{

super(context);

}

public ActionEditText(Context context, AttributeSet attrs)

{

super(context, attrs);

}

public ActionEditText(Context context, AttributeSet attrs, int defStyle)

{

super(context, attrs, defStyle);

}

@Override

public InputConnection onCreateInputConnection(EditorInfo outAttrs)

{

InputConnection conn = super.onCreateInputConnection(outAttrs);

outAttrs.imeOptions &= ~EditorInfo.IME_FLAG_NO_ENTER_ACTION;

return conn;

}

}

请注意,某些inputType等选项如textShortMessage使这不起作用! 我建议你从inputType="text"开始。以下是如何在XML中使用它。

android:id=...

android:layout_stuff=...

android:imeOptions="actionDone"

android:inputType="textAutoCorrect|textCapSentences|textMultiLine"

android:maxLines="3" />

Timmmm answered 2019-04-11T17:11:24Z

55 votes

子类化EditText类的另一种解决方案是使用以下方法配置EditText实例:

editText.setHorizontallyScrolling(false);

editText.setMaxLines(Integer.MAX_VALUE);

至少,这适用于Android 4.0。 它配置EditText实例,以便用户编辑单线字符串,即使设置了IME操作,也会在多行上进行软包装。

Futzilogik answered 2019-04-11T17:11:58Z

6 votes

按照上一个回答

public class MultiLineText extends EditText {

public MultiLineText(Context context) {

super(context);

}

public MultiLineText(Context context, AttributeSet attrs) {

super(context, attrs);

}

public MultiLineText(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

}

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

}

}

使用它就像

android:id="@+id/textNotes"

android:layout_height="wrap_content"

android:minHeight="100dp"

android:layout_width="wrap_content"

android:hint="Notes"

android:textSize="20sp"

android:padding="7dp"

android:maxLines="4"/>

user3525689 answered 2019-04-11T17:12:31Z

3 votes

为了把动作完成,你可以使用:

XML

android:inputType="text|textCapSentences"

JAVA

editText.setHorizontallyScrolling(false);

editText.setMaxLines(Integer.MAX_VALUE);

我希望它为你工作。

Alex Zaraos answered 2019-04-11T17:13:04Z

1 votes

显然原始问题的答案是肯定的,但我相信Android团队正试图让开发人员思考他们如何使用多行EditText。 他们希望输入键添加换行符,并且可能希望您提供一个按钮或其他输入方法来引发您已完成编辑的事件。

我有同样的问题,我的明显解决方案只是添加一个完成按钮,让enter按钮添加换行符。

Mullins answered 2019-04-11T17:13:37Z

0 votes

在XML中使用这些属性。

安卓的inputType=“textImeMultiLine”

机器人:imeOptions=“actionDone”

Gaurav answered 2019-04-11T17:14:18Z

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值