如何才能不关闭键盘上完成时键盘是pressed键盘、pressed

2023-09-07 15:41:19 作者:心锁了雾.

在完成软键盘上的用户presses,键盘关闭。我想它,以便它只会关闭,如果某个条件为真(如密码输入正确)。

这是我的code(设置了完成当一个监听器按钮pressed):

 最后的EditText等=(EditText上)findViewById(R.id.et);et.setOnEditorActionListener(新OnEditorActionListener(){   @覆盖   公共布尔onEditorAction(TextView的V,INT actionId,KeyEvent的事件)   {      如果(actionId == EditorInfo.IME_ACTION_DONE)      {         如果(et.getText()。的toString()。等于(密码))//他们进入正确的         {             //记录他们在         }         其他         {             //调出键盘             getWindow()。setSoftInputMode(             WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);             Toast.makeText(Main.this,不正确的,Toast.LENGTH_SHORT).show();         }      }      返回false;   }}); 
电脑键盘显示不了,是怎么回事

我知道这不工作的原因可能是因为它的实际关闭软键盘对自己之前运行此code的,但是这就是为什么我需要帮助。我不知道另一种方式。

答案一个可能的议题可以与合作:

  activityRootView.getViewTreeObserver()。addOnGlobalLayoutListener(新OnGlobalLayoutListener(){ 

和诸如此类的事情,但我不知道。

__________________________________________________________________________________

解决方案:

 的EditText等=(EditText上)findViewById(R.id.et);et.setOnEditorActionListener(新OnEditorActionListener(){  @覆盖  公共布尔onEditorAction(TextView的V,INT actionId,KeyEvent的事件)  {    如果(actionId == EditorInfo.IME_ACTION_DONE)    {       如果(et.getText()。的toString()。等于(密码))//他们进入正确的       {           //记录他们在           返回false; //关闭键盘       }       其他       {           Toast.makeText(Main.this,不正确的,Toast.LENGTH_SHORT).show();           返回true; //保持键盘向上       }    }    //如果你没有在上面的,如果结构中的return语句,你    //可以把返回true;这里要始终保持键盘的上,当DONE    //动作pssed $ P $。但与上述返回语句,不要紧    返回false; //或返回true  }}); 

解决方案

如果您的回归真正 onEditorAction 方法,动作不会被再次处理。在这种情况下,你可以返回真正来毫不掩饰键盘的时候动作 EditorInfo.IME_ACTION_DONE

When the user presses "Done" on the soft keyboard, the keyboard closes. I want it so that it will only close if a certain condition is true (eg. the password was entered correctly).

This is my code (sets up a listener for when the "Done" button is pressed):

final EditText et = (EditText)findViewById(R.id.et);
et.setOnEditorActionListener(new OnEditorActionListener() 
{        
   @Override
   public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
   {
      if(actionId==EditorInfo.IME_ACTION_DONE)
      {
         if (et.getText().toString().equals(password)) // they entered correct
         {
             // log them in
         }
         else
         {
             // bring up the keyboard
             getWindow().setSoftInputMode(
             WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

             Toast.makeText(Main.this, "Incorrect.", Toast.LENGTH_SHORT).show();
         }
      }
      return false;
   }
});

I realize that the reason this doesn't work is probably because it runs this code before it actually closes the soft keyboard on its own, but that's why I need help. I don't know another way.

A possible topic for answers could be working with:

activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 

and that sort of thing, but I don't know for sure.

__________________________________________________________________________________

SOLUTION:

EditText et = (EditText)findViewById(R.id.et);
et.setOnEditorActionListener(new OnEditorActionListener() 
{        
  @Override
  public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
  {
    if(actionId==EditorInfo.IME_ACTION_DONE)
    {
       if (et.getText().toString().equals(password)) // 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
       }
    }
    // if you don't have the return statements in the if structure above, you
    // could put return true; here to always keep the keyboard up when the "DONE"
    // action is pressed. But with the return statements above, it doesn't matter
    return false; // or return true
  }
});

解决方案

if your return true from your onEditorAction method, action is not going to be handled again. In this case you can return true to not hide keyboard when action is EditorInfo.IME_ACTION_DONE.