Ci potrebbero essere tre modi per raggiungere questo obiettivo:
Impostare android:hint
su TextInputLayout
su un carattere _
e mantenere android:hint="This is my cool hint"
impostato su EditText
.
<android.support.design.widget.TextInputLayout
....
....
android:hint=" "> <<----------
<EditText
....
....
android:hint="This is my cool hint"/> <<----------
</android.support.design.widget.TextInputLayout>
Questo funziona perché TextInputLayout
esegue il seguente controllo prima di utilizzare il EditText's
suggerimento:
// If we do not have a valid hint, try and retrieve it from the EditText
if (TextUtils.isEmpty(mHint)) {
setHint(mEditText.getHint());
// Clear the EditText's hint as we will display it ourselves
mEditText.setHint(null);
}
Impostando android:hint=" "
, if (TextUtils.isEmpty(mHint))
restituisce false
, e il EditText
mantiene il suo suggerimento.
Seconda opzione sarebbe quella di sottoclasse TextInputLayout
e sovrascrivere la sua addView(View child, int index, ViewGroup.LayoutParams params)
metodo:
public class CTextInputLayout extends TextInputLayout {
public CTextInputLayout(Context context) {
this(context, null);
}
public CTextInputLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CTextInputLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void addView(View child, int index, ViewGroup.LayoutParams params) {
if (child instanceof EditText) {
// cache the actual hint
CharSequence hint = ((EditText)child).getHint();
// remove the hint for now - we don't want TextInputLayout to see it
((EditText)child).setHint(null);
// let `TextInputLayout` do its thing
super.addView(child, index, params);
// finally, set the hint back
((EditText)child).setHint(hint);
} else {
// Carry on adding the View...
super.addView(child, index, params);
}
}
}
quindi utilizzare il vostro personalizzato CTextInoutLayout
al posto di quello della libreria di supporto di progettazione:
<your.package.name.CTextInputLayout
....
.... > <<----------
<EditText
....
....
android:hint="This is my cool hint"/> <<----------
</your.package.name.CTextInputLayout>
In terzo luogo, e probabilmente il modo più diretto sarebbe effettuare le seguenti chiamate:
// remove hint from `TextInputLayout`
((TextInputLayout)findViewById(R.id.textContainer)).setHint(null);
// set the hint back on the `EditText`
// The passed `String` could also be a string resource
((EditText)findViewById(R.id.myEditText)).setHint("This is my cool hinttt.");
idea migliore per programatically devi dare suggerimento e ora TextChange dare al suggerimento nulla in editbox questa idea può aiutare a –
problema è il colore suggerimento. Diventa bianco quando si digita qualcosa. Si prega di cambiare il colore suggerimento o cambiare il colore di sfondo. Vedrai il suggerimento durante la digitazione. –