有没有办法在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" />
输出:
没有获得关注:
有没有办法让浮动提示中的星号与浮动提示颜色的颜色不同?
答案 0 :(得分:1)
您可以使用android.support.design.widget.TextInputEditText
。
<android.support.design.widget.TextInputLayout
android:id="@+id/textInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingEnd="16dp"
android:paddingStart="16dp"
android:paddingTop="16dp">
<android.support.design.widget.TextInputEditText
android:id="@+id/et2"
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>
并将范围设置为Edittext
。
TextInputLayout textInput = findViewById(R.id.textInput);
Spannable wordtoSpan = new SpannableString("Hello world");
wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 2, 6,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textInput.setHint(wordtoSpan);
EditText et2=findViewById(R.id.et2);
et2.setHint(wordtoSpan);
这将使Hint可以浮动。默认情况下,提示将使用样式中定义的颜色浮动colorAccent
。
如果你想保持Floated Hint的颜色为Spannable。我觉得这不是那么微不足道。
如果上述答案对您不起作用。此外,我无法做出工作,也许它非常微不足道,但我现在无法弄清楚。 作为解决方案,您可以尝试https://github.com/rengwuxian/MaterialEditText。我已经根据需要对它进行了测试。试试看:
<com.rengwuxian.materialedittext.MaterialEditText
android:id="@+id/et3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
app:met_textColor="@android:color/black"
app:met_baseColor="@android:color/black"
app:met_floatingLabel="highlight"
app:met_maxCharacters="20"
app:met_floatingLabelTextColor="@color/colorAccent"
android:layout_margin="7dp"
app:met_primaryColor="@color/colorAccent"
app:met_singleLineEllipsis="true"
/>
并将提示设置为:
Spannable wordtoSpan = new SpannableString("Hello world");
wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLACK), 0, wordtoSpan.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 2, 6,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
MaterialEditText et3=findViewById(R.id.et3);
et3.setFloatingLabelText(wordtoSpan);
et3.setHint(wordtoSpan);