はじめに
こんにちは!
レイアウトに作成しているときにEditTextについて考えさせられたので、
EditTextのあれこれということでまとめます!
EditTextだけでこれだけカスタムできるんだーと驚きました。笑
まとめ
EditTextの色変更箇所まとめ のように表にすると見やすいなーと思ったので、同じように表でまとめようと思います!
コードの確認
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/edit_txt"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:paddingStart="10dp"
android:background="@drawable/edit_text_background"
android:hint="10文字以内で入力してね!!(Hint)"
android:maxLength="10"
android:textColor="#FF33CC"
android:textColorHint="#339900"
android:theme="@style/EditTextStyle"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
<!-- android:theme="@style/EditTextStyle"で読み込んでいる -->
<style name="EditTextStyle">
<item name="colorControlActivated">#006400</item>
<item name="android:textCursorDrawable">@drawable/edit_text_cursor</item>
<item name="android:textColorHighlight">#800080</item>
</style>
<!-- <item name="android:textCursorDrawable">@drawable/edit_text_cursor</item>で読み込んでいる -->
<!-- カーソルレイアウト -->
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<size android:width="2dp" />
<solid android:color="#FFFF00" />
</shape>
<!-- android:background="@drawable/edit_text_background"で読み込んでいる -->
<!-- バックグラウンド -->
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:width="4dp"
android:color="#FF0000" />
<solid android:color="#fff" />
</shape>
ちょっと補足
android:theme
でstyleを読み込んでいます。android:textColor
android:textColorHint
をstyle.xmlに記載して、themeで呼び出そうとすると文字が透明で色がつかないと言う不具合が起きたのでstyle.xmlにまとめませんでした。(原因まではわかっていないのと、もしかしたら端末依存?の可能性もありますね。理由はわかっていないです。
android:textCursorDrawable
にカラーコードを入力すると色は変更されるが、点滅が起こらなくなります。なので、動きを変えたくない場合はdrawableで指定した方が良いです。(デフォルトは点滅あり)
android:background
はdrawableで呼び出すことによりカスタマイズは大幅に変えることができます。デフォルトのままだと下線部しか出てこないので少しダサいです。。
ハンドラーの下に線が出てくる不具合が起きた場合
EditTextを実装している時にハンドラーの下に線が出てくるという不具合が起きました。
(青矢印で差している部分です!)
不具合が起こった時の画像
不具合解消時の画像
原因としてはstyles.xml
styleの parent が原因でした。
継承することで影響があるということですが何が影響しているかは分かりません。ですが今回に関してはparentは不要なのでparentを削除することにより問題は解決しました。
<!-- 修正前(不具合あり) -->
<style name="EditTextStyle" parent="Widget.AppCompat.EditText">
<item name="colorControlActivated">#006400</item>
<item name="android:textCursorDrawable">@drawable/edit_text_cursor</item>
<item name="android:textColorHighlight">#800080</item>
</style>
<!-- 修正後(不具合解消) -->
<style name="EditTextStyle">
<item name="colorControlActivated">#006400</item>
<item name="android:textCursorDrawable">@drawable/edit_text_cursor</item>
<item name="android:textColorHighlight">#800080</item>
</style>
最後に
現在はxmlを使用しないレイアウト作成もありますが、
xmlの使用もされているので、まとめてみました。
ユーザーに見えるところなので、こういうこともできるんだと知っておくと良いですね!
最後までご覧いただきありがとうございました!
参考
EditTextの色変更箇所まとめ
EditTextのthemeを設定したらhandle部分に謎の下線が出てきたときの対処法