플랫폼/안드로이드 android

[Android] EditText 자동 포커스 제거

삐뚤어진 개발자 2020. 2. 13.

안드로이드에서 화면 액티비티가 뜰때, EditText에 자동으로 포커스가 가는 경우가 있다.

 

(화면이 뜰때 뿐만아니라 브로드 캐스트 리턴 액션으로 화면이 리프레시 될 때, 자동으로 포커스가 가는 현상이 발생할 수 있다. )

 

그럴때 간단한 해결방법이 있다.

 

 

xml로 변경

 

아래와 같이 EditText를 감싸는 LinearLayout에 속성

android:focusable="true"

android:focusableInTouchMode="true"

두 가지를 추가해준다.  (터치를 해야 포커스가 되는 모드를 true로 설정해주는 모양이다.)

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true">

	<EditText
                android:id="@+id/header_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10"
                android:inputType="textPersonName" />


</LinearLayout>

 

자바 소스로 변경

위의 xml에 추가 하는 방법도 있지만 자바 소스에서 LinearLayout을 선언해 소스로 추가 해줄 수도 있다.

 

setFocusable(true);
setFocusableInTouchMode(true);

이 두가지다.

자세한 사용 방법은..리니어 레이아웃 객체를 만들어 함수를 사용하면 된다.

LinearLayout linear = findViewById(R.id.linear); // 리니어 레이아웃 객체를 만들어 주고 찾는다

linear.setFocusable(true);
linear.setFocusableInTouchMode(true);

 

 

안드로이드 EditText 자동 포커스 제거

댓글