Gooday Note Scrap Source Android login
 
작성일 : 12-06-28 09:27
EditText에 자동으로 포커스(Focus)되는것 막기
 글쓴이 : 기리
조회 : 3,317   추천 : 0   비추천 : 0  
   http://theeye.pe.kr/499 [840]
화면에서 EditText가 있을 경우 엑티비티가 생성되면서 자동으로 포커스가 가는 경우가 있습니다. 사실 경우가 있다기 보다는 무조건 포커스가 갑니다. 이것이 좋게 느껴지지는 않는군요. 자동으로 생기는 포커스를 제거하기 위해서는 다양한 방법을 사용할 수 있지만 다음의 방법이 가장 간단하지 않을까 생각합니다.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <EditText
                android:id="@+id/editText"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"/>

</LinearLayout>

위에서 볼 수 있듯이 가장 바깥에 있는 LinearLayout에 다음의 두개의 옵션을 추가하는것만으로EditText에 포커스가 자동으로 가는것을 막을 수 있습니다.
android:focusable="true"
android
:focusableInTouchMode="true"

위의 옵션은 가장 바깥쪽 엘리먼트인 LinearLayout이 우선적으로 포커스를 받아버림으로써 EditText로 포커스가 가는것을 막는 설정입니다. 원래 LinearLayout은 포커스를 받는 대상이 아니지만 포커스를 받게함과 동시에 LinearLayout은 특성상 포커스를 받아도 티가 나지 않기 때문에 별 문제 없이 사용할 수 있습니다.

만약에 위와 같은 구조로 화면이 구성되지 않아 위의 방법을 구현하기 힘들경우 눈에 보이지 않는 LinearLayout을 하나 추가해 주시면 됩니다.
<LinearLayout
        android:id="@+id/linearLayout_focus"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:layout_width="0px"
        android:layout_height="0px"/>

예전에는 엑티비티가 뜨는 순간부터 오른쪽의 모습을 가지던것이 왼쪽과 같이 자동으로 포커스가 가는것이 사라졌습니다.
사용자 삽입 이미지사용자 삽입 이미지
[좌 : 자동포커스 제거 후 / 우 : 자동포커스 제거 전]



참고: http://stackoverflow.com/questions/1555109/stop-edittext-from-gaining-focus-at-activity-startup
크리에이티브 커먼즈 라이센스
Creative Commons License