1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Android】EditTextのあれこれ

Last updated at Posted at 2023-05-07

はじめに

こんにちは!
レイアウトに作成しているときにEditTextについて考えさせられたので、
EditTextのあれこれということでまとめます!
EditTextだけでこれだけカスタムできるんだーと驚きました。笑

まとめ

EditTextの色変更箇所まとめ のように表にすると見やすいなーと思ったので、同じように表でまとめようと思います!

変更箇所 属性 説明
android:textColorHighlight テキストを選択色のこと。
フォーマットはカラーコード
colorControlActivated handleの色を変更する。
フォーマットはカラーコード
android:textColor 入力した文字列の色を変更する。
フォーマットはカラーコード
android:textColorHint 入力前のヒントの文字列の色を変更する。
フォーマットはカラーコード
android:textCursorDrawable カーソルの色を変更します。
drawableまたはカラーコードをセットすることができましたが、動きが異なりました。
android:background 入力エリアを自身のレイアウトを決める
drawableまたはカラーコードをセットすることができる。drawableにするとカスタマイズが広い

コードの確認

activity_main.xml
<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>
styles.xml
<!-- 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>
edit_text_cursor.xml
<!-- <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>
edit_text_background
<!-- 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:textColorandroid:textColorHintをstyle.xmlに記載して、themeで呼び出そうとすると文字が透明で色がつかないと言う不具合が起きたのでstyle.xmlにまとめませんでした。(原因まではわかっていないのと、もしかしたら端末依存?の可能性もありますね。理由はわかっていないです。
android:textCursorDrawableにカラーコードを入力すると色は変更されるが、点滅が起こらなくなります。なので、動きを変えたくない場合はdrawableで指定した方が良いです。(デフォルトは点滅あり)
android:backgroundはdrawableで呼び出すことによりカスタマイズは大幅に変えることができます。デフォルトのままだと下線部しか出てこないので少しダサいです。。

ハンドラーの下に線が出てくる不具合が起きた場合

EditTextを実装している時にハンドラーの下に線が出てくるという不具合が起きました。
(青矢印で差している部分です!)
不具合が起こった時の画像

不具合解消時の画像

原因としてはstyles.xmlstyleの parent が原因でした。
継承することで影響があるということですが何が影響しているかは分かりません。ですが今回に関してはparentは不要なのでparentを削除することにより問題は解決しました。

styles.xml
<!-- 修正前(不具合あり) -->
<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>
styles.xml
<!-- 修正後(不具合解消) -->
<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部分に謎の下線が出てきたときの対処法

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?