如何使用红色星号标记

时间:2017-04-04 19:07:32

标签: android android-layout

我正在尝试使用TextInputLayout创建必填字段。我使用了Spannable类,但是以单色显示TextInputLayout的提示。我希望我的视图看起来像下面的链接。

https://material.io/guidelines/components/text-fields.html#text-fields-search-filter

1 个答案:

答案 0 :(得分:0)

我创建了以下TextInputEditText,使其看起来与Material Design Text Field尽可能相似。

XML布局

            <android.support.design.widget.TextInputLayout
            android:id="@+id/etlayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:counterEnabled="true"
            app:counterMaxLength="25">

            <android.support.design.widget.TextInputEditText
                android:id="@+id/et"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingTop="16dp"
                android:layout_marginLeft="-4dp"
                android:layout_marginStart="-4dp"
                android:layout_marginTop="8dp"
                android:textSize="16sp"
                android:inputType="text|textCapSentences"
                android:singleLine="true"
                android:hint="@string/title"
                android:textColorHint="@color/transparent38"/>

        </android.support.design.widget.TextInputLayout>

颜色

<color name="transparent38">#66000000</color>

爪哇

etlayout = (TextInputLayout)findviewbyid(R.id.etlayout);

        TextWatcher twTitle = new TextWatcher() { //Analyzes when the view changes
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (true) { //Replace condition with validity test
                etlayout.setErrorEnabled(false); //Removes the extra space
                etlayout.setError(null); //Removes the error message
            } else { //If the text is not valid
                etlayout.setErrorEnabled(true); //Makes the space for the error appear
                etlayout.setError("Invalid input"); //Shows a message
            }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        };
        etTitle.addTextChangedListener(twTitle);