티스토리 뷰

반응형

Stack Overflow에 자주 검색, 등록되는 문제들과 제가 개발 중 찾아 본 문제들 중에서 나중에도 찾아 볼 것 같은 문제들을 정리하고 있습니다.

Stack Overflow에서 가장 먼저 확인하게 되는 가장 높은 점수를 받은 Solution과 현 시점에 도움이 될 수 있는 가장 최근에 업데이트(최소 점수 확보)된 Solution을 각각 정리하였습니다.

 

아래 word cloud를 통해 이번 포스팅의 주요 키워드를 미리 확인하세요.

How to show soft-keyboard when edittext is focused

EditText에 포커스가 맞춰지면 소프트 키보드를 어떻게 보여줄까요?

 문제 내용 

I want to automatically show the soft-keyboard when an EditText is focused (if the device does not have a physical keyboard) and I have two problems:

EditText에 포커스가 맞추어지면 자동으로 소프트 키보드를 보여주고 싶습니다(물리적 키보드가 없는 경우). 그런데 두 가지 문제가 있습니다.

 

  1. When my Activity is displayed, my EditText is focused but the keyboard is not displayed, I need to click again on it to show the keyboard (it should be displayed when my Activity is displayed).
  2. And when I click done on the keyboard, the keyboard is dissmissed but the EditText stays focused and y don't want (because my edit is done).
1. Activity가 표시되면 EditText에 포커스가 맞추어지지만 키보드가 표시되지 않습니다. 키보드를 보여주려면 다시 클릭해야합니다 (Activity가 표시될 때 키보드가 표시되어야합니다).

2. 키보드에서 done을 클릭하면 키보드는 없어지지만 EditText는 여전히 포커스가 유지됩니다(수정이 완료되었기 때문에 포커스를 끄고 싶습니다).

 

To resume, my problem is to have something more like on the iPhone: which keep the keyboard sync with my EditText state (focused / not focused) and of course does not present a soft-keyboard if there is a physical one.

요약하자면, iPhone의 경우와 같이 EditText 상태 (포커스 / 포커스 없음)에 따라 키보드를 유지하면서 물리적 키보드가 없는 경우에만 소프트 키보드가 표시되어야합니다.

 

 

 

 높은 점수를 받은 Solution 

To force the soft keyboard to appear, you can use

소프트 키보드를 강제로 나타내려면 다음을 사용할 수 있습니다.
EditText yourEditText= (EditText) findViewById(R.id.yourEditText);
yourEditText.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);

 

And for removing the focus on EditText, sadly you need to have a dummy View to grab focus.

그리고 EditText에서 포커스를 제거하려면 더미 View를 사용하여 포커스를 얻어야합니다.

 


To close it you can use

닫으려면 다음을 사용할 수 있습니다.
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(yourEditText.getWindowToken(), 0);

This works for using it in a dialog

이것은 대화 상자에서 사용하는 것입니다.
public void showKeyboard(){
    InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
    inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}

public void closeKeyboard(){
    InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
    inputMethodManager.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
}

 

 

 가장 최근 달린 Solution 

Here's a more reliable solution i got from Square:

Square에서 가져온 더욱 신뢰할 수 있는 해결책은 다음과 같습니다.

 

fun View.focusAndShowKeyboard() {
   /**
    * This is to be called when the window already has focus.
    */
   fun View.showTheKeyboardNow() {
       if (isFocused) {
           post {
               // We still post the call, just in case we are being notified of the windows focus
               // but InputMethodManager didn't get properly setup yet.
               val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
               imm.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT)
           }
       }
   }

   requestFocus()
   if (hasWindowFocus()) {
       // No need to wait for the window to get focus.
       showTheKeyboardNow()
   } else {
       // We need to wait until the window gets focus.
       viewTreeObserver.addOnWindowFocusChangeListener(
           object : ViewTreeObserver.OnWindowFocusChangeListener {
               override fun onWindowFocusChanged(hasFocus: Boolean) {
                   // This notification will arrive just before the InputMethodManager gets set up.
                   if (hasFocus) {
                       this@focusAndShowKeyboard.showTheKeyboardNow()
                       // It’s very important to remove this listener once we are done.
                       viewTreeObserver.removeOnWindowFocusChangeListener(this)
                   }
               }
           })
   }
}

 

Code credits from here.

코드 출처는 여기에 있습니다.

 

 

 

출처 : https://stackoverflow.com/questions/5105354/how-to-show-soft-keyboard-when-edittext-is-focused

반응형
댓글
공지사항
최근에 올라온 글