다음 스 니펫은 단순히 키보드를 숨 깁니다.
public static void hideSoftKeyboard(Activity activity) {
InputMethodManager inputMethodManager =
(InputMethodManager) activity.getSystemService(
Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(
activity.getCurrentFocus().getWindowToken(), 0);
}
이것을 유틸리티 클래스에 넣거나 활동 내에서 정의하는 경우 활동 매개 변수를 피하거나을 호출하십시오 hideSoftKeyboard(this).
가장 까다로운 부분은 언제 전화해야하는지입니다. View활동의 모든 과정을 반복하는 메소드를 작성하고 해당 컴포넌트에 instanceof EditText등록되어 있지 않은지 확인 setOnTouchListener하고 모든 것이 제자리에 있는지 확인하십시오 . 그렇게하는 방법이 궁금하다면 실제로 매우 간단합니다. 여기에 당신이하는 일이 있습니다. 다음과 같은 재귀 적 방법을 작성하십시오. 실제로이를 사용하여 사용자 정의 서체 설정과 같은 것을 할 수 있습니다 ... 여기에 방법이 있습니다
public void setupUI(View view) {
// Set up touch listener for non-text box views to hide keyboard.
if (!(view instanceof EditText)) {
view.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
hideSoftKeyboard(MyActivity.this);
return false;
}
});
}
//If a layout container, iterate over children and seed recursion.
if (view instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
View innerView = ((ViewGroup) view).getChildAt(i);
setupUI(innerView);
}
}
}
그게 전부 setContentView입니다. 활동을 마친 후에이 메소드를 호출 하십시오. 전달할 매개 변수가 궁금한 경우 id상위 컨테이너의 매개 변수 입니다. 다음 id과 같이 부모 컨테이너에
<RelativeLayoutPanel android:id="@+id/parent"> ... </RelativeLayout>
전화 setupUI(findViewById(R.id.parent)), 그게 다야.
이를 효과적으로 사용하려면 확장을 작성 Activity하고이 메소드를 삽입하고 애플리케이션의 다른 모든 활동이이 활동을 확장 setupUI()하고 onCreate()메소드 에서 호출하도록 할 수 있습니다.
도움이 되길 바랍니다.
둘 이상의 활동을 사용하는 경우 공통 ID를 상위 레이아웃에 정의하십시오.
<RelativeLayout android:id="@+id/main_parent"> ... </RelativeLayout>
그런 다음 클래스를 확장하고 클래스 내 에서 Activity정의 하고``활동 대신이 클래스를 확장하십시오.setupUI(findViewById(R.id.main_parent))OnResume()in your program
위 함수의 Kotlin 버전은 다음과 같습니다.
@file:JvmName("KeyboardUtils")
fun Activity.hideSoftKeyboard() {
currentFocus?.let {
val inputMethodManager = ContextCompat.getSystemService(this, InputMethodManager::class.java)!!
inputMethodManager.hideSoftInputFromWindow(it.windowToken, 0)
}
}