답변:
가상 키보드를 비활성화 또는 해제 하시겠습니까?
그냥 닫으려면 버튼 클릭 이벤트에서 다음 코드 줄을 사용할 수 있습니다.
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
getActivity())을 getCurrentFocus().getWindowToken()사용할 수 hideSoftInputFromWindow()있습니다. 또한 활동을 변경할 때 사라지게하려는 경우가 onPause()아닌 onStop()작업을 수행하십시오.
위의 솔루션은 모든 장치에서 작동하지 않으며 EditText를 매개 변수로 사용합니다. 이것은 내 솔루션입니다.이 간단한 방법을 호출하십시오.
private void hideSoftKeyBoard() {
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
if(imm.isAcceptingText()) { // verify if the soft keyboard is open
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
}
isAcceptingText()이 답변을 다른 사람보다 낫게 함
이것은 나의 해결책이다
public static void hideKeyboard(Activity activity) {
View v = activity.getWindow().getCurrentFocus();
if (v != null) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}
다음은 Kotlin 솔루션입니다 (다양한 답변을 스레드에 혼합)
확장 함수 생성 (아마도 일반적인 ViewHelpers 클래스에서)
fun Activity.dismissKeyboard() {
val inputMethodManager = getSystemService( Context.INPUT_METHOD_SERVICE ) as InputMethodManager
if( inputMethodManager.isAcceptingText )
inputMethodManager.hideSoftInputFromWindow( this.currentFocus.windowToken, /*flags:*/ 0)
}
그런 다음 간단히 다음을 사용하여 소비하십시오.
// from activity
this.dismissKeyboard()
// from fragment
activity.dismissKeyboard()
InputMethodManager가있는 첫 번째 솔루션은 나를 위해 챔피언처럼 작동했습니다 .getWindow (). setSoftInputMode 메소드는 Android 4.0.3 HTC Amaze에서 작동하지 않았습니다.
@Ethan Allen은 편집 텍스트를 최종적으로 만들 필요가 없었습니다. 아마도 포함 메소드를 선언 한 EditText 내부 클래스를 사용하고 있습니까? EditText를 Activity의 클래스 변수로 만들 수 있습니다. 또는 내부 클래스 / 메소드 안에 새 EditText를 선언하고 findViewById ()를 다시 사용하십시오. 또한 양식의 어떤 EditText에 포커스가 있는지 알아야합니다. 나는 임의로 하나를 골라서 사용할 수 있습니다. 이렇게 :
EditText myEditText= (EditText) findViewById(R.id.anyEditTextInForm);
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
public static void hideSoftInput(Activity activity) {
try {
if (activity == null || activity.isFinishing()) return;
Window window = activity.getWindow();
if (window == null) return;
View view = window.getCurrentFocus();
//give decorView a chance
if (view == null) view = window.getDecorView();
if (view == null) return;
InputMethodManager imm = (InputMethodManager) activity.getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm == null || !imm.isActive()) return;
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
} catch (Throwable e) {
e.printStackTrace();
}
}
이 솔루션은 키보드를 숨기지 않은 경우 키보드를 숨기지 않아도 아무것도하지 않도록합니다. 확장을 사용하므로 모든 컨텍스트 소유자 클래스에서 사용할 수 있습니다.
fun Context.dismissKeyboard() {
val imm by lazy { this.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager }
val windowHeightMethod = InputMethodManager::class.java.getMethod("getInputMethodWindowVisibleHeight")
val height = windowHeightMethod.invoke(imm) as Int
if (height > 0) {
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0)
}
}
관점의 맥락을 사용함으로써 Kotlin에서 다음 확장 방법으로 원하는 결과를 얻을 수 있습니다.
/**
* Get the [InputMethodManager] using some [Context].
*/
fun Context.getInputMethodManager(): InputMethodManager {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return getSystemService(InputMethodManager::class.java)
}
return getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
}
/**
* Dismiss soft input (keyboard) from the window using a [View] context.
*/
fun View.dismissKeyboard() = context
.getInputMethodManager()
.hideSoftInputFromWindow(
windowToken
, 0
)
이것들이 제정되면 다음을 호출하십시오.
editTextFoo.dismiss()