android - 隐藏和显示软键盘 - 同时

标签 android keyboard

如果我可以在这里从 this topic 提出几乎相同的问题

我在我的 activity_main.xml 文件中添加了:

 android:focusable="true"
 android:focusableInTouchMode="true"

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
tools:context="com.example.stefancvetkovic.stefantest001.MainActivity">

<EditText
    android:id="@+id/txtScanedResult"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="textPersonName"
    tools:layout_editor_absoluteX="16dp"
    tools:layout_editor_absoluteY="16dp" />
</android.support.constraint.ConstraintLayout>

它工作正常,但是当我想在完成事件中隐藏我的键盘时,键盘保持打开状态。

主要 Activity .java:

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ((EditText)findViewById(R.id.txtScanedResult)).setOnEditorActionListener(
            new EditText.OnEditorActionListener() {
                @Override
                public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                    if (actionId == EditorInfo.IME_ACTION_SEARCH ||
                            actionId == EditorInfo.IME_ACTION_DONE ||
                            event != null &&
                                    event.getAction() == KeyEvent.ACTION_DOWN &&
                                    event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
                        if (event == null || !event.isShiftPressed()) {

                            // the user is done typing.
                                //HIDE KEYBOARD
                            EditText edtView=(EditText)findViewById(R.id.txtScanedResult);
                            edtView.setInputType((InputType.TYPE_NULL));
                                //
                            Toast.makeText(getApplicationContext(),"Voila!",Toast.LENGTH_SHORT)
                                    .show();
                            return true; // consume.
                        }
                    }
                    return false; // pass on to other listeners.
                }
            });
}

Toast 在完成事件时完美运行,但键盘保持打开状态。 我如何设法在加载时关闭键盘,并在 finishEvent 上隐藏? 我在 Android 5.1 上的模拟器中运行

最佳答案

试试这个

/**
     * This function is used to hide soft keyboard
     *
     * @param context mContext
     * @param view    view for which keyboard is open
     */
    public static void hideSoftInput(Context context, View view) {
        if (view != null) {
            InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
            if (!inputMethodManager.isActive()) {
                return;
            }
            inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }

    /**
     * This function is used to hide soft keyboard
     *
     * @param activity activity
     */
    public static void hideSoftInput(Activity activity) {
        try {
            if (activity != null) {
                View focusedView = activity.getCurrentFocus();
                hideSoftInput(activity, focusedView);
            }
        } catch (Throwable t) {
            CustomLogHandler.printErrorlog(t);
        }
    }

    /**
     * This function is used to show soft keyboard
     *
     * @param activity activity
     */
    public static void showSoftInput(Activity activity) {
        try {
            if (activity != null) {
                View focusedView = activity.getCurrentFocus();
                if (focusedView != null) {
                    InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
                    inputMethodManager.showSoftInput(focusedView, InputMethodManager.SHOW_IMPLICIT);
                }
            }
        } catch (Throwable t) {
            CustomLogHandler.printErrorlog(t);
        }
    }

    /**
     * This function is used to show soft keyboard
     *
     * @param view view for which soft keyboard need to be opened
     */
    public static void showSoftInput(final View view) {
        try {
            if (view == null) {
                return;
            }

                    view.requestFocus();
                    InputMethodManager inputManager = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                    inputManager.showSoftInput(view, 0);

        } catch (Exception e) {
            CustomLogHandler.printErrorlog(e);
        }
    }

或者试试

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

关于android - 隐藏和显示软键盘 - 同时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48766463/

相关文章:

Android ListView 单击上下文菜单

android - 以编程方式获取谷歌玻璃热

lua - 用Lua模拟键盘输入

iPhone AlertView 文本字段键盘不可见

隐藏在键盘显示上的Android布局

android - Activity 标志 ScreenOrientation.Portrait 不存在

安卓导出数据到csv

android - 试图弄清楚如何获取 Android 的触摸事件

objective-c - Cocos2d - Mac : Check for keyboard events?

iPhone:UITextField 结束编辑事件不会隐藏键盘