如何在 EditText 上的 OK 上隐藏软键盘并再次显示

How to hide the soft keyboard on OK on an EditText and show it again

我在 fragment 中有一个数字 EditText,当我 select EditText 时键盘显示正常。我想在输入确定时隐藏键盘。所以我使用了一个 hide_keyboard() 函数,它工作正常。

我遇到的问题是,当我重新 select EditText 时,软键盘不再显示。我尝试了很多东西,但 none 成功了。

有什么想法吗?

这是我的 EditText:

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:ems="7"
    android:id="@+id/kip_time"
    android:hint="Reflexion time"
    android:layout_below="@+id/chronometer_kipling"
    android:layout_alignStart="@+id/chronometer_kipling"
    android:layout_marginTop="10dp"
    />

和我的 hide_keyboard() 函数:

   private void hide_keyboard(Context context, View view) {
        InputMethodManager inputManager = (InputMethodManager)
                context.getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.toggleSoftInput(0, 0);
    }

最后是我的 onclicklistener 方法:

   kip_time.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            reflexion_time = Integer.parseInt(kip_time.getText().toString());
            reflexion_time = reflexion_time * 1000;
            hide_keyboard(context, view);
        }
    });

使用以下隐藏键盘的方法,并检查当您单击editText时是否可以再次显示键盘:

private void hideKeypad() {
        View view = context.getCurrentFocus();

        InputMethodManager inputManager = (InputMethodManager) context
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        if (view instanceof EditText) {
            inputManager.hideSoftInputFromWindow(context
                    .getCurrentFocus().getWindowToken(),
                    InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

如果您需要在输入值后隐藏键盘,那么只需使用

android:imeOptions="actionDone"

它在软键盘上提供了一个'done'按钮,用户可以在完成输入值后单击该按钮。将此添加到您的 EditText 声明并删除您的 hide_keyboard() 函数。 如下更新布局 xml。

<EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:ems="7"
        android:id="@+id/kip_time"
        android:hint="Reflexion time"
        android:layout_below="@+id/chronometer_kipling"
        android:layout_alignStart="@+id/chronometer_kipling"
        android:imeOptions="actionDone"
        android:layout_marginTop="10dp"
        />

* 处理完成按钮点击事件使用监听器如下 *

kip_time.setOnKeyListener(new OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {

                if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
                    (keyCode == KeyEvent.KEYCODE_ENTER)) {
                    // you codes gose here
                    //reflexion_time = Integer.parseInt(kip_time.getText().toString());
                    //reflexion_time = reflexion_time * 1000;
                  return true;
                }
                return false;
            }
        });

我解决了我的问题:

   kip_time.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_NUMPAD_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE)) {
                Log.i("OK", "Enter pressed");
                reflexion_time = Integer.parseInt(kip_time.getText().toString());
                reflexion_time = reflexion_time * 1000;
                hide_keyboard();
            }
            return false;
        }
    });

使用隐藏键盘:

   private void hide_keyboard() {
        InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(kip_time.getWindowToken(), 0);
    }

不需要 show_keyboard()

如果不是多行输入,直接在EditText中添加这个即可

android:singleLine="true"

用户只需按 Enter/OK 软键即可...