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

nar*_*arb 3 keyboard android android-edittext

我有一个数字EditTextfragment显示键盘为正常,当我选择EditText.我想在输入OK时隐藏键盘.所以我使用了hide_keyboard()函数,它正常工作.

我遇到的问题是当我重新选择时EditText,软键盘不再出现了.我尝试过很多东西,但都没有用.

有任何想法吗?

这是我的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"
    />
Run Code Online (Sandbox Code Playgroud)

和我的hide_keyboard()函数:

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

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

Nir*_*han 6

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

机器人:imeOptions = "actionDone"

它在软键盘上提供了一个"完成"按钮,用户可以在输入值时单击.将其添加到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"
        />
Run Code Online (Sandbox Code Playgroud)

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

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


nar*_*arb 5

我通过执行以下操作解决了我的问题:

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

使用隐藏键盘:

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

不需要 show_keyboard()