我有一个不允许换行的多行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" />
答案 0 :(得分:26)
我建议阅读这篇文章
非常好的例子
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 } }
答案 1 :(得分:20)
android:inputType="textEmailAddress|textEmailSubject"
您需要将输入类型设置为电子邮件地址或电子邮件主题。任何一个都会给你你想要的结果。 shouldAdvanceFocusOnEnter()
是TextView中的私有方法,用于确定是输入新行还是将焦点移至下一个字段。
答案 2 :(得分:5)
如果您在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"
的任何设置,这非常奇怪且令人困惑。
答案 3 :(得分:4)
我为带有actionLabel的多行文本执行此操作:
editText.setSingleLine(true);
editText.setLines(10);
editText.setHorizontallyScrolling(false);
editText.setImeActionLabel(getString(R.string.ready), 0);