【问题标题】:How to hide Keypad without Clicking Back Button [duplicate]如何在不单击后退按钮的情况下隐藏键盘[重复]
【发布时间】:2017-09-20 16:28:27
【问题描述】:

我在我的应用程序中使用了一个编辑文本,一旦我完成了键盘想要自动隐藏而不按返回按钮的输入。谁能帮帮我....

【问题讨论】:

标签: android


【解决方案1】:

在您的Edittext 中尝试此代码,您将获得自行关闭键盘的选项....

android:imeOptions="actionDone"

【讨论】:

    【解决方案2】:

    您应该使用 TextWatcher 知道您何时完成输入,然后您可以隐藏键盘,如下所示:

     EditText editText;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        editText = (EditText)findViewById(R.id.editText);
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
            }
    
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
    
                if(count == 5){
    
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);}
    
            }
    
            @Override
            public void afterTextChanged(Editable s) {
    
            }
        });
    
    }
    

    所以在这段代码中输入五个字符后键盘会自动隐藏。

    试试看。

    【讨论】:

      【解决方案3】:

      只要输入完成就调用这个函数

      InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
      //Hide:
      imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
      
      private void hideKeyboard() {   
          // Check if no view has focus:
          View view = this.getCurrentFocus();
          if (view != null) {
              InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
              inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
          }
      }
      

      【讨论】:

        【解决方案4】:
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        
        editText.requestFocus();
        
        imm.showSoftInput(editText, 0);
        

        试试这个(在editText中你应该放你自己的editText)。

        【讨论】:

          猜你喜欢
          • 2012-04-30
          • 2012-11-15
          • 1970-01-01
          • 2021-03-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多