본문 바로가기
개발/Android

[Android] EditText 자동 포커스 제거

by blacktree 2022. 11. 25.
반응형

안녕하세요

 

Activity가 뜨면서 EditText에 자동 포커스가 될 때가 있다. 

어떻게 하면 EditText 자동 포커스 제거를 하는 방법에 대해서 찾아봤습니다. 

 

여기서는 2가지 방법에 대해서 알아보겠다. 

 

 

1.  xml 파일에서 EditText 자동 포커스 제거

  1. EditText의 상위 레이아웃이나 뷰에  아래의 옵션을 추가한다. 
  2. android:focusableInTouchMode="true"
    android:focusable="true"
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    android:focusableInTouchMode="true"
    android:focusable="true"
    android:orientation="vertical">

 

2. Class에서  EditText 자동 포커스 제거

  1. 소스상에 포커스를 줄 layout을 선언하고 아래와 같이 옵션을 준다.
  2. java
    1. setFocusable(true);
    2. setFocusableInTouchMode(true);
  3. kotlin
    1. mLayout.isFocusableInTouchMode = true
    2. mLayout.isFocusable = true
val mLayout : LinearLayout = rootView.findViewById(R.id.mLayout)
mLayout.isFocusableInTouchMode = true
mLayout.isFocusable = true

 

위와 같은 방법으로 원하지 않는 포커스 즉 EditText 자동 포커스 제거를 할 수 있다. 

 

 

 

 

728x90
반응형

댓글