textinputedittext studio personalizar ocultar mostrar formularios example edittext diseños developer contraseña android android-edittext material-design android-textinputlayout

studio - personalizar edittext en android



Cómo hacer que TextInputLayout hint asterisk red para los campos requeridos (6)

¿Podría estar agregando una cadena distribuida desde HTML?

String grey = "Legal first name*"; Spanned redStar; String html = "<string style="color:grey;">Legal first name<span style="color:red;">*</span></string>"; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) { redStar = Html.fromHtml(html,Html.FROM_HTML_MODE_LEGACY); } else { redStar = Html.fromHtml(html); }

Y cambia la pista sobre el enfoque.

textInput.setOnFocusChangeListener(new View.OnFocusChangeListener() { public void onFocusChange(View v, boolean hasFocus) { if (hasFocus) myEditText.setHint(redStar.toString); else myEditText.setHint(grey); }

¿Es posible hacer que solo el asterisco en la pista sea rojo cuando se utiliza un TextInputLayout de la biblioteca de soporte de diseño? He visto información sobre el estilo de toda la pista, pero esto es un poco más complejo ya que solo el * debería ser rojo, no todo el mensaje.

El ejemplo de Diseño de materiales muestra esto, pero la biblioteca de diseño no parece tener ninguna opción para personalizarlo de esta manera utilizando TextInputLayout y EditText.

Referencia: https://www.google.com/design/spec/components/text-fields.html#text-fields-required-fields

Ejemplo (la parte superior, con el foco, tiene el asterisco rojo; la parte inferior sin el foco no tiene un asterisco rojo):

Busqué en la configuración de la pista en un SpannableString (ver aquí Cómo obtener un asterisco rojo en una entrada <string> ) en un OnFocusChangeListener (ver aquí Tener el símbolo obligatorio para editar texto (asterisco de color rojo) que está dentro del textoinputlayout ) , pero la pista es una charSequence.

¿Hay alguna manera de hacer esto sin extender TextInputLayout?


Cambie la sugerencia TextInputLayout agregando un postfix envuelto en html:

public void setRequired(boolean required) { componentField.setHint( TextUtils.concat( componentField.getHint(), Html.fromHtml( getContext().getString( R.string.required_asterisk)))); }

El código de asterisco se debe almacenar en strings.xml de Android para que se escape correctamente:

<string name = "required_asterisk"><![CDATA[<font color=/'#cc0029/'>&nbsp*</font>]]></string>


El Diseño de materiales especifica un asterisco para los campos requeridos que forman parte del texto de la sugerencia y del mismo color (no en rojo como se muestra en su pregunta).

En Kotlin esto es realmente simple:

1. Definir este método de extensión:

fun TextInputLayout.markRequired() { hint = "$hint *" }

2.Utilizalo:

input_first_name.markRequired()


He pasado por el mismo proceso y funcionó para mí.

TextView text = (TextView) rootView.findViewById(R.id.name_textview); SpannableStringBuilder builder1 = setStarToLabel("Name"); text.setText(builder1); @NonNull private SpannableStringBuilder setStarToLabel(String text) { String simple = text; String colored = "*"; SpannableStringBuilder builder = new SpannableStringBuilder(); builder.append(simple); int start = builder.length(); builder.append(colored); int end = builder.length(); builder.setSpan(new ForegroundColorSpan(Color.RED), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); return builder; }


Sí, es posible, porque estoy haciendo lo mismo en mi aplicación actual. y para enfocar y desenfocar tienes que hacer algo de lógica para eliminar un asterisco.

String mm = "Legal first name"; Spannable WordtoSpan = new SpannableString(mm); WordtoSpan.setSpan( new ForegroundColorSpan(Color.parseColor("#e62e6c")), 16, mm.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); WordtoSpan.setSpan(new RelativeSizeSpan(0.9f), 16, mm.length(), 0); view.setText(WordtoSpan);


<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/black</item> </style>

Cambia tu tema colorAccent y mira