AndroidはどのようにEditTextをフォーカスさせ、文字の末尾にカーソルを合わせます(キーボードのイジェクトと回収を含む)

8163 ワード

コード:
   editText.setFocusable(true);
   editText.setFocusableInTouchMode(true);
   editText.requestFocus();
   editText.setSelection(editText.getText().length());

携帯電話のソフトキーボードをイジェクトします.
 getWindow().getDecorView().post(() -> {
       InputMethodManager inputManager =
       (InputMethodManager) this.getSystemService(
            Context.INPUT_METHOD_SERVICE);
       inputManager.showSoftInput(remarkEditText, 0);
        });

携帯電話の画面の空白をクリックして、ソフトキーボードを閉じます:
 //          
    private void initListener() {
        // mRootView      view 
        mRootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              if (getCurrentFocus() != null){
               hideKeyboard(mRootView,getCurrentFocus().getWindowToken());
                }
            }
        });
    }
    private void hideKeyboard(final View view, IBinder windowToken) {
        if(windowToken == null){
            return;
        }
        InputMethodManager inputMethodManager = (InputMethodManager) view.getContext().getApplicationContext()
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        if (inputMethodManager == null){
            return;
        }
        boolean active = inputMethodManager.isActive();
        if (active) {
            inputMethodManager.hideSoftInputFromWindow(windowToken, 0);
        }
    }