【发布时间】:2014-12-26 00:49:49
【问题描述】:
我在 SO 上搜索了六个其他答案,但没有找到一个有效的答案。我要做的就是在用户按下回车按钮时关闭软键盘。 (相当于极其简单的 iOS 'resignKeyboard' 调用。)在下面的代码中,不会调用 onEditorAction 方法。我在我的 XML 文件中设置了一个 EditText 视图,我的片段中的代码如下:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
fragView = inflater.inflate(R.layout.fragment_encrypt2, container, false);
textField = (EditText) fragView.findViewById(R.id.textField);
textField.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView arg0, int actionId,
KeyEvent arg2) {
// hide the keyboard and search the web when the enter key
// button is pressed
if (actionId == EditorInfo.IME_ACTION_GO
|| actionId == EditorInfo.IME_ACTION_DONE
|| actionId == EditorInfo.IME_ACTION_NEXT
|| actionId == EditorInfo.IME_ACTION_SEND
|| actionId == EditorInfo.IME_ACTION_SEARCH
|| (arg2.getAction() == KeyEvent.KEYCODE_ENTER)) {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(textField.getWindowToken(), 0);
return true;
}
return false;
}
});
// Inflate the layout for this fragment
return fragView;//inflater.inflate(R.layout.fragment_encrypt2, container, false);
}
以下是我定义 EditText 字段的 XML 文件中的 sn-p。我需要 EditText 是多行的。
<EditText
android:id="@+id/textField"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="@string/Encrypted_Text_Hint"
android:gravity="top"
android:inputType="textMultiLine"
android:imeOptions="actionDone"/>
【问题讨论】:
-
你没有在
EditText上设置任何imeOptions,在这种情况下return被认为是另一个输入字符。 -
我已经设置了imeOptions,但是我需要EditText是多行的
标签: android actionlistener android-softkeyboard android-input-method