【Android】起動時にEditTextにフォーカスが当たるのを防ぐ

【Android】起動時にEditTextにフォーカスが当たるのを防ぐ

2015-05-28

画面起動時やダイアログを表示したときにEditTextなどを使用していた時にデフォルトでフォーカスがEditTextのパーツにあたってしまうのを防ぐ方法

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_weight="1"
        android:orientation="horizontal" >


        <EditText
            android:id="@+id/dispNum"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/edittext_base"
            android:ems="10"
            android:gravity="right"
            android:inputType="number"
            android:padding="5dp"
            android:singleLine="true"
            android:textSize="24sp" >
        </EditText>


        <Button
            android:id="@+id/okButton"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="2dp"
            android:layout_weight="1"
            android:background="@drawable/button_submit"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:text="@string/calc_ok"
            android:textColor="#FFFFFF" >


            <requestFocus />
        </Button>
    </LinearLayout>

今回はButtonにフォーカスを当てるようにして防いでいます。
TextViewとかでも代用できるので最悪ダミーで置いておいて使うしかなさそう。
他にもフォーカスを外す方法がありそうだけどこのやり方がどの端末においても安定してそうだった。