按返回键时隐藏软键盘

cph*_*117 31 android actionlistener android-softkeyboard android-input-method

我在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);
    }
Run Code Online (Sandbox Code Playgroud)

以下是我定义EditText字段的XML文件的片段.我需要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"/>
Run Code Online (Sandbox Code Playgroud)

Geo*_*mas 71

如果您不想要多行,对于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"
   />
Run Code Online (Sandbox Code Playgroud)

我显然不知道你是否想要实现这个目标.任何方式都可以看看.

编辑: android:singleLine自API 3以来因性能不佳而被弃用,你必须使用android:maxLines.singleLine将可用,因为即使现在android:maxLines属性也不支持某些效果.

  • Multiline 和 android:imeOptions="actionDone" 似乎不能同时工作 (2认同)

cph*_*117 9

我通过更改以下XML文件中的以下内容解决了该问题:

 android:inputType="textMultiLine"
Run Code Online (Sandbox Code Playgroud)

至:

 android:inputType="textImeMultiLine"
Run Code Online (Sandbox Code Playgroud)


Ket*_*tel 8

下面的代码适合我.

IN XML:

    android:imeOptions="actionDone"
    android:inputType="textCapWords"
Run Code Online (Sandbox Code Playgroud)

在JAVA CLASS:

    edit_text.setOnEditorActionListener(new OnEditorActionListener() {

                @Override
                public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                    // TODO Auto-generated method stub

                    if ((actionId==EditorInfo.IME_ACTION_DONE )   )
                     {   
                        //Toast.makeText(getActivity(), "call",45).show();
                       // hide virtual keyboard
                       InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);

                       //or try following:
                       //InputMethodManager imm = (InputMethodManager)getBaseContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                       imm.hideSoftInputFromWindow(auto_data.getWindowToken(), 0);
                       return true;
                    }
                    return false;

                }
            });
Run Code Online (Sandbox Code Playgroud)


小智 5

根据我的测试,当用户单击 Enter 按钮时,关闭软键盘真正需要的是什么,只需将以下代码添加到 xml 中的 EditText 即可。

android:imeOptions="actionDone"
android:inputType="textImeMultiLine"
Run Code Online (Sandbox Code Playgroud)

不需要 OnEditorActionListener 或任何其他 Java 代码。