스택오버플로우에서 찾은 여러 방법 중 이게 제일 깔끔하게 작동했다..

 

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    View view = getCurrentFocus();
    if (view != null && (ev.getAction() == MotionEvent.ACTION_UP || ev.getAction() == MotionEvent.ACTION_MOVE) && view instanceof EditText && !view.getClass().getName().startsWith("android.webkit.")) {
        int scrcoords[] = new int[2];
        view.getLocationOnScreen(scrcoords);
        float x = ev.getRawX() + view.getLeft() - scrcoords[0];
        float y = ev.getRawY() + view.getTop() - scrcoords[1];
        if (x < view.getLeft() || x > view.getRight() || y < view.getTop() || y > view.getBottom())
        ((InputMethodManager)this.getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow((this.getWindow().getDecorView().getApplicationWindowToken()), 0);
    }
    return super.dispatchTouchEvent(ev);
}

 

출처:https://stackoverflow.com/questions/8697499/hide-keyboard-when-user-taps-anywhere-else-on-the-screen-in-android/8697635

 

hide keyboard when user taps anywhere else on the screen in android

I need to hide the softkeypad in android when user click on anywhere other than a Edittext. There are many help for iphone but not for android. I tried this code but its not working :( final

stackoverflow.com

 

액티비티 레이아웃 XML파일에 아래와 같이 추가하면 된다.

        <com.google.android.material.textfield.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:passwordToggleEnabled="true">

            <EditText
                android:id="@+id/signupActivity_edittext_password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="PASSWORD"
                android:inputType="textPassword" />
        </com.google.android.material.textfield.TextInputLayout>

app:passwordToggleEnabled="true"을 EditText를 감싸고 있는 레이아웃에 추가해준다.

 

+ Recent posts