有没有办法在不影响浮动行为的情况下为EditText
提示设置多种颜色?android.support.design.widget.TextInputLayout
EditText Hint
** 例如:** 标题* 黑色的“标题”和红色的“*”
从字面上看,我的问题与这个问题完全相同:Multicolored edittext hint。
我再次发布它,因为该问题没有有效的答案。(请不要将我的问题标记为与那个重复的问题!!)
我已经尝试过那里给出的答案,这是结果:
您可以看到 的浮动属性TextInputLayout
不再适用。(希望提示像这样浮动)
无论如何要保持TextInputLayout EditText
多色提示的行为?
代码:
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingEnd="16dp"
android:paddingStart="16dp"
android:paddingTop="16dp">
<EditText
android:id="@+id/appointment_patient_name_et_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:inputType="textPersonName"
android:textColor="@color/colorAccent" />
</android.support.design.widget.TextInputLayout>
尝试设置多色提示:
appointment_patient_name_et = findViewById(R.id.appointment_patient_name_et_id);
Spannable wordtoSpan = new SpannableString("Hello world");
wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 2, 6,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
appointment_patient_name_et.setHint(wordtoSpan);
编辑 1: 使用 ADM 建议的 git 库后...
.java:
MaterialEditText appointment_patient_name_et;
appointment_patient_name_et = findViewById(R.id.appointment_patient_name_et_id);
// Multicolor hint
final Spannable spannable1 = new SpannableString("Patient's Name *");
spannable1.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.colorPrimary)), 0,
spannable1.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable1.setSpan(new ForegroundColorSpan(Color.RED),
spannable1.length() - 1, spannable1.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Spannable spannable2 = new SpannableString("Patient's Name *");
spannable2.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.colorAccent)), 0,
spannable2.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable2.setSpan(new ForegroundColorSpan(Color.RED),
spannable2.length() - 1, spannable2.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
appointment_patient_name_et.setHint(spannable1);
appointment_patient_name_et.setFloatingLabelText(spannable2);
appointment_patient_name_et.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
appointment_patient_name_et.setFloatingLabelAlwaysShown(true);
appointment_patient_name_et.setHint("");
} else {
appointment_patient_name_et.setFloatingLabelAlwaysShown(false);
appointment_patient_name_et.setHint(spannable1);
}
}
});
.xml:
<com.rengwuxian.materialedittext.MaterialEditText
android:id="@+id/appointment_patient_name_et_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="32dp"
android:layout_marginStart="32dp"
android:layout_marginTop="16dp"
android:hint="Patient's Name *"
app:met_baseColor="@color/colorAccent"
app:met_clearButton="true"
app:met_floatingLabel="highlight"
app:met_floatingLabelTextColor="@color/colorAccent"
app:met_primaryColor="@color/colorAccent"
app:met_singleLineEllipsis="true"
app:met_textColor="@color/colorAccent"
app:met_textColorHint="@color/colorPrimary" />
输出:
没有获得焦点:
有没有办法让浮动提示中的星号与浮动提示颜色不同?