微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – 如果按下键盘上的DONE,如何禁止关闭键盘

用户在软键盘上按“完成”时,键盘关闭.我想要它,只有在一定条件为真时才会关闭(例如,密码输入正确).

这是我的代码(当按下“完成”按钮时设置一个监听器):

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

我意识到,这不行的原因可能是因为它运行这个代码之前,它实际上关闭了自己的软键盘,但这就是为什么我需要帮助.我不知道另一种方式.

一个可能的答案主题可能在于:

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

那种事情,但我不知道肯定.

解:

EditText et = (EditText)findViewById(R.id.et);
et.setonEditorActionListener(new OnEditorActionListener() 
{        
  @Override
  public boolean onEditorAction(TextView v,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,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
  }
});

解决方法

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

原文地址:https://www.jb51.cc/java/126681.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐