android - 按下返回键时隐藏软键盘

标签 android actionlistener android-softkeyboard android-input-method

我在 SO 上搜索了六个其他答案,但没有找到一个有效的答案。我要做的就是在用户按下回车按钮时关闭软键盘。 (相当于极其简单的 iOS 'resignKeyboard' 调用。)在以下代码中,不会调用 onEditorAction 方法。我在我的 XML 文件中设置了一个 EditText View ,我的 fragment 中的代码如下:

@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 文件的 fragment 。我需要 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 指定单行。对于edittext,您也可以像这样将imeOptions设置为Done:

<EditText 
   android:id="@+id/edittext_done"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:imeOptions="actionDone"
   android:singleLine="true"
   />

我显然不知道,您是否正在努力实现这一目标。任何方式看看。

编辑: android:singleLine 由于性能不佳,自 API 3 起已弃用,您必须改用 android:maxLines。 singleLine 将可用,因为即使是现在,android:maxLines 属性也不支持某些效果。

关于android - 按下返回键时隐藏软键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26645212/

相关文章:

java - 在矩形内移动小球程序

android - 我的 Activity 开始时如何显示电话输入软键盘?

android - 如何开发PhoneGap android的 "Zxing"条码扫描器应用

java - 如何将对象添加到另一个类Java中的数组

java - Xml解析嵌套标签返回null

android - 在特定任务中使用 NFC 启动 Activity

java - 了解 ActionListener

java - 如何使用 ActionListener 更改 JTable

android - 显示没有动画的键盘

android - 带有 edittext 的 DialogFragment 在解雇后不会丢失键盘