Android 安卓:即使单击“完成”按钮,也要保持软键盘打开

Android 安卓:即使单击“完成”按钮,也要保持软键盘打开,android,Android,在这里,单击done按钮,软键盘会自动关闭,但我想让它保持打开状态 下面是onCreate()方法中的当前代码 final InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY); e

在这里,单击
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;
    }
});
试试这个代码

如果从onEditorAction方法返回true,则不会再次处理该操作。在这种情况下,当操作为EditorInfo.IME\u action\u 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;
        }
    });

当检测到done键时,只需在重写的onKey方法中插入此代码段

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

试试这个,很简单

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;
            }
        });

试试这个

这对你肯定有帮助

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;
        }
    });

如果从重写的方法
onEditorAction
返回
true
,系统将不会再次处理该操作。所以,在这种情况下,当操作是
EditorInfo.IME\u action\u 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;
      }
 });