AndroidのEditTextの警告への対処
Androidアプリ開発時ののEditTextに発生した警告への対処方法を記す。
導入
Android Studio 3.5.3でminSdkVersion=15, targetSdkVersion=29でEditTextを使ったアプリを作成していた。
すると,Gitのコミット直前のCode Analysisや [Anlyze]>[Inspect Code] の [Lint] でSDKのバージョンに由来する警告が2個出た。
EditTextを使う度に毎回同じ警告がでるのがうっとうしいので,対処方法を記す。
android:hint
まず1個目はandroid:hint属性に対する以下の警告だ。
Missing accessibility label: where minSdk 17, you should provide an 'android:hint'
minSdk 17からandroid:hintが使えるようになったので,アクセシビリティの観点からこれを指定するように指摘されている。
そこで,android:hintを指定する。ただし,空 (android:hint="") だと以下の警告が出る。
Empty 'android:hint' attribute
そのため,strings.xmlに記載した内容を指定する。プレースホルダーなので,何を入力すればいいかわかるような情報を記入すればよいだろう。
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="text"
android:hint="@string/hint" />これでこの警告に対処できた。
android:autofillHints
2個目はandroid:autofillHints属性に関する以下の警告だ。
Warning:(11, 6) Missing `autofillHints` attribute
EditTextにautofillHints属性がないことを指摘された。特にautofillHintsは必要ないのだが,指摘を消したいので以下の通りにandroid:autofillHints=""の値を空で指定した。
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="text"
android:hint="@string/hint"
android:autofillHints="" />しかし,今度は別の指摘を受けた。
Warning:(12, 9) Attribute `autofillHints` is only used in API level 26 and higher (current min is 15)
autofillHintsはAPIレベル26以上でしか使えないらしい。対応に困って調べたところ,「android – “Missing autofillHints attribute” – Stack Overflow」が参考になった。
minSdk >= 26にできるならば話が簡単だが,そうはいかないので,minSdk < 26の方法を取る。
autofillHintsを使わない場合,android:autofillHints="" tools:targetApi="o" を指定する。元記事では,「tools:ignore="Autofill" または,android:importantForAutofill="no" tools:targetApi="o" を指定する。」とあるが,使用時と形式を同じにできるのでこちらでいいだろう。
autofillHintsを使う場合,android:autofillHints="" tools:targetApi="o" を指定する。
autofillFintsにはstring.xmlの文字列の他に,定数を指定できる。基本的には定数を使ったほうがいい。
使える定数は「setAutofillHints」で受け入れ可能な以下となる。
AUTOFILL_HINT_USERNAME
AUTOFILL_HINT_PASSWORD
AUTOFILL_HINT_EMAIL_ADDRESS
AUTOFILL_HINT_NAME
AUTOFILL_HINT_PHONE
AUTOFILL_HINT_POSTAL_ADDRESS
AUTOFILL_HINT_POSTAL_CODE
AUTOFILL_HINT_CREDIT_CARD_NUMBER
AUTOFILL_HINT_CREDIT_CARD_SECURITY_CODE
AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_DATE
AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_DAY
AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_MONTH
AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_YEAR前半のAUTOFILL_HINT_の部分を取り除いて,_も取り除き,2文字目からの単語の先頭を大文字にして指定する (例: postalAddress)。EditTextやinputType属性がこれらの内容に一致するなら定数を使うとよいだろう。一致しなければ,自前で実装するか空文字 ("") を指定するとよいだろう。
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="text"
android:hint="@string/hint"
android:autofillHints=""
tools:targetApi="o" />ここで登場したtools属性は「ツール属性のリファレンス | Android Studio | Android Developers」で解説されている。ビルド時のビルドツールに削除されるため,APKやランタイムの動作には影響ないらしい。
tools:targetApiは値にAPIレベル (整数またはコード名) を指定でき,ツールに指定したAPI以上でのみ使用されることを伝える。これにより,minSdkVersionで対応していなくてもlintから警告が出なくなる。
結論
最終的に,EditTextの末尾に以下の3属性を追加することで警告に対応できた。
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="text"
android:hint="@string/hint"
android:autofillHints=""
tools:targetApi="o" />警告への対応が高品質なアプリにつながるので,見つけたらすぐに解消していきたい。


“Androidの
EditTextの警告への対処” に対して1件のコメントがあります。