Androidの起動直後のEditText
のフォーカスの予防

Androidアプリの開発でEditText
を配置した状態でアプリを起動させると,勝手にEditText
にフォーカスがあたり,キーボードが表示される。これが鬱陶しいので抑制する。
いくつか方法がある。「listview – How to stop EditText from gaining focus at Activity startup in Android – Stack Overflow」の回答でAndroid Studio 3.5.3上でうまくできた。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true" >
上記の通り,EditText
の親のレイアウト要素に以下の2属性を指定する。
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
詳細は不明だがこれで解決できた。また似たような問題に遭遇したら,再調査してこの投稿に情報を追加することにする。
リンク