-1

有没有办法在不影响浮动行为的情况下为EditText提示设置多种颜色?android.support.design.widget.TextInputLayoutEditText 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" />

输出:
没有获得焦点:

在此处输入图像描述

获得焦点后:
在此处输入图像描述

有没有办法让浮动提示中的星号与浮动提示颜色不同?

4

1 回答 1

0

您可以使用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);

这将使提示可以浮动。默认情况下,提示将使用 style 中定义的颜色浮动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);
于 2018-06-15T15:18:20.093 回答