Android:即使点击完成按钮,也可以保持软键盘打开

时间:2016-12-14 07:01:44

标签: android

enter image description here

在此,通过单击done按钮,软键盘会自动关闭,但我想将其保持打开状态。

以下是onCreate()方法中的当前代码。

final InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);

etPIN.setOnKeyListener(new View.OnKeyListener() {
    @Override
    public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
        if((keyEvent.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
            String pin1 = etPIN.getText().toString();
            Toast.makeText(PINActivity.this, pin1, Toast.LENGTH_SHORT).show();

            tvPINGuide.setText(getString(R.string.confirm_pin));
            etPIN.setText("");
        }
        return false;
    }
});

5 个答案:

答案 0 :(得分:2)

如果您从覆盖的方法true返回onEditorAction,则 ystem将无法再次处理该操作。因此,在这种情况下,如果操作为EditorInfo.IME_ACTION_DONE,则应返回true以不隐藏键盘。

这里使用此代码:

edittext.setOnEditorActionListener(new TextView.OnEditorActionListener() {
      @Override
      public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
          if (actionId == EditorInfo.IME_ACTION_DONE) {

          }
          return true;
      }
 });

答案 1 :(得分:0)

试试此代码

如果从onEditorAction方法返回true,则不会再次处理操作。在这种情况下,当action为editorInfo.IME_ACTION_DONE时,您可以返回true以不隐藏键盘。

    editText = (EditText) findViewById(R.id.edit_text);
    editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                // do your stuff here
if (editText.getText().toString().equals(PIN)) // they entered correct
       {
           // log them in
           return false; // close the keyboard
       }
       else
       {
           Toast.makeText(Main.this, "Incorrect.", Toast.LENGTH_SHORT).show();
           return true; // keep the keyboard up
       }
            }
            return false;
        }
    });

答案 2 :(得分:0)

只需在检测到完成密钥时将此代码段插入覆盖的onKey方法

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.showSoftInput(view,InputMethodManager.SHOW_FORCED);

来源https://developer.android.com/reference/android/view/inputmethod/InputMethodManager.html

答案 3 :(得分:0)

Try this , its simple

etPIN.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                  //do your stuff here
                }
                return true;
            }
        });

答案 4 :(得分:0)

试试这个

它肯定会帮助你

etPIN.setOnKeyListener(new View.OnKeyListener() 
{
        @Override
        public boolean onKey(View view, int keyCode, KeyEvent keyEvent)
        {
            if((keyEvent.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER))
            {
                String pin1 = etPIN.getText().toString();
                Toast.makeText(PINActivity.this, pin1, Toast.LENGTH_SHORT).show();

                tvPINGuide.setText(getString(R.string.confirm_pin));
                etPIN.setText("");
            }
            if (keyEvent.getKeyCode() == KeyEvent.FLAG_EDITOR_ACTION) // when done button pressed
            {
                // it will open your keyboard again here
                InputMethodManager inputMethodManager =  (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
                inputMethodManager.toggleSoftInputFromWindow(etPIN.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);
                etPIN.requestFocus();
            }
            return false;
        }
    });