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

时间:2015-12-16 09:53:25

标签: android keyboard android-edittext

我在UPDATE IP_ADMISSION_REQUEST ip1 SET IP1.WRIST_BAND_PRINT_STATUS=0 WHERE IP1.IP_ADM_REQ_ID = (SELECT IP.IP_ADM_REQ_ID FROM IP_ADMISSION_REQUEST ip INNER JOIN VISIT v ON ip.ip_visit_id=v.visit_id AND v.pat_id =3702 ); `enter code here` 中有一个数字EditText,当我选择fragment时,键盘正常显示。我想在输入OK时隐藏键盘。所以我使用 hide_keyboard()函数,它正常工作。

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

有什么想法吗?

这是我的EditText:

EditText

和我的hide_keyboard()函数:

<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"
    />

最后是我的onclicklistener方法:

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

4 个答案:

答案 0 :(得分:6)

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

  

机器人:imeOptions =&#34; actionDone&#34;

它提供了一个完成的&#39;软键盘上的按钮,用户可以在输入值时单击。将其添加到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;
            }
        });

答案 1 :(得分:4)

我通过以下方式修复了我的问题:

   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()

答案 2 :(得分:0)

使用以下方法隐藏键盘并检查单击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);
        }
    }

答案 3 :(得分:-1)

如果它不是多行输入,只需将其添加到EditText

即可
android:singleLine="true"

用户只需在软键上输入/确定选项即可......