TextInputLayout框颜色

时间:2019-05-24 11:45:07

标签: android android-layout

使用以下xml元素,我可以将TextInputLayout框正确地涂成白色,但这仅是在单击它之后。初始颜色仍然是默认颜色。

public class TableControllerStudent extends DatabaseHandler {

public TableControllerStudent(Context context) {
    super(context);
}

public boolean create(ObjectStudent objectStudent) {

    ContentValues values = new ContentValues();


  /*  values.put("word", objectStudent.word);
    values.put("meaning", objectStudent.meaning);
    values.put("details", objectStudent.details);
    values.put("lesson", objectStudent.lesson);*/

    values.put("word", "w");
    values.put("meaning", "m");
    values.put("details", "d");
    values.put("lesson", "l");

    values.put("ticks", objectStudent.ticks);

    SQLiteDatabase db = this.getWritableDatabase();

    db.execSQL("INSERT INTO " + "words "+ "(word, meaning,details, lesson, ticks ) VALUES ('word','meaning','details','lesson',2)");

    //boolean createSuccessful = db.insert("words", null, values) > 0;
    db.close();

    //return createSuccessful;
    return true;
}

有没有一种方法可以使框和提示文本着色,而不仅是在真正赋予TextInput焦点之后才应用?

2 个答案:

答案 0 :(得分:0)

在res / color / text_input_box_stroke.xml中创建Selector,内容如下:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="#fcc" android:state_focused="true"/>
    <item android:color="#cfc" android:state_hovered="true"/>
    <item android:color="#ccf"/>
</selector>

然后将您的styles.xml中的内容放入:

<style name="TextInputLayoutStyle" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense">
    <item name="boxStrokeColor">@color/text_input_box_stroke</item>
    <item name="boxStrokeWidth">2dp</item>
</style>

最后,在您的TextInputLayout

中使用该样式
<com.google.android.material.textfield.TextInputLayout
    style="@style/TextInputLayoutStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:hintTextColor="@color/colorWhite"
    app:boxStrokeColor="@color/colorWhite"
    app:boxBackgroundColor="@color/colorPrimaryLight">
    ........
</com.google.android.material.textfield.TextInputLayout>

在您的text_input_box_color中添加color.xml

<color name="mtrl_textinput_default_box_stroke_color" tools:override="true">#fff</color>

有关更多详细信息,请检查here

答案 1 :(得分:0)

要更改TextInputLayout中的颜色,只需使用以下命令:

  <style name="OutlinedBoxColor" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
    <!-- border color in OutlinedBox
    <item name="boxStrokeColor">@color/text_input_layout_stroke_color</item>

    <!-- The color of the label when it is collapsed and the text field is active -->
    <item name="hintTextColor">@color/singleColor</item>
    <!-- The color of the label in all other text field states (such as resting and disabled) -->
    <item name="android:textColorHint">@color/.....</item>
  </style>

TextInputLayout被聚焦而不被聚焦时的结果 enter image description here enter image description here

您应该为这些颜色使用颜色选择器:

对于boxStrokeColor

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:color="?attr/colorPrimary" android:state_focused="true"/>
  <item android:alpha="0.87" android:color="?attr/colorOnSurface" android:state_hovered="true"/>
  <item android:alpha="0.12" android:color="?attr/colorOnSurface" android:state_enabled="false"/>
  <item android:alpha="0.38" android:color="?attr/colorOnSurface"/>
</selector>

对于android:textColorHint

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:alpha="0.38" android:color="?attr/colorOnSurface" android:state_enabled="false"/>
  <item android:alpha="0.6" android:color="?attr/colorOnSurface"/>
</selector>