¿Cómo establecer el ancho mínimo fijo de EditText para que se ajuste firmemente alrededor de la pista y el contenido limitado por duración limitada?

Quiero fijar el ancho fijo mínimo para un EditText modo que pueda contener su indirecta pero también el contenido mecanografiado, longitud-limitado como un número de 2 dígitos.

Algunos detalles:

  1. Quiero ser capaz de hacer esto dinámicamente ya que tengo numerosos campos para diferentes propósitos con diferentes pistas (en diferentes idiomas) y longitud de entrada (unos 2 dígitos, otros 4).
  2. Las sugerencias no son necesariamente más largas que la propia entrada. Una sugerencia podría ser "dd" o "Día" y la entrada podría ser un número de dígito.
  3. No necesito espacio para sugerencia y contenido al mismo tiempo; Las sugerencias desaparecen cuando el usuario comienza a escribir.
  4. Estoy usando fuentes personalizadas en una clase extendida de EditText , pero eso se debe manejar mientras estoy copiando la EditText de EditText .

Tengo un método de utilidad para hacerlo, pero devuelve un ancho que es demasiado estrecho por lo que la pista se recorta. ¿Qué estoy haciendo mal?

El EditText se especifica en XML como este:

  <EditText android:id="@+id/birthday_month" android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="number" android:hint="@string/birthday_month_hint" android:lines="1" android:maxLength="2"> 

En mi actividad encuentro primero el EditText y luego lo preparo usando Texts.setNiceAndTightWidth(monthEditText, 2) definido a continuación (incluyendo los métodos auxiliares):

 public class Texts { public static void setNiceAndTightWidth ( EditText editText, int maxInputLength ) { // String of chars to test for widest char. Include all possible input chars and chars of hint, as we need to make room for hint as well. String testChars = String.format("1234568790%s", editText.getHint().toString()); char widestChar = getWidestChar(editText, testChars); String widestString = repeat(widestChar, maxInputLength); float widestStringWidth = getTextWidth(editText, widestString); int width = (int)(widestStringWidth + 0.5f); editText.setWidth(width); // This was an experiment but it doesn't help. ViewGroup.LayoutParams lp = editText.getLayoutParams(); lp.width = width; editText.setLayoutParams(lp); } public static char getWidestChar ( TextView textView, String testChars ) { float width, widest = 0; char widestChar = '\0'; // Using Paint properties of TextView, including Fontface, text size, etc. Paint paint = new Paint( textView.getPaint() ); for ( int i = 0 ; i < testChars.length() ; i++ ) { width = paint.measureText(testChars, i, i+1); if ( width > widest ) { widest = width; widestChar = testChars.charAt(i); } } return widestChar; } public static String repeat ( char ch, int length ) { char[] chars = new char[length]; Arrays.fill(chars, ch); String string = String.valueOf(chars); return string; } public static float getTextWidth ( TextView textView, CharSequence text ) { Paint paint = new Paint( textView.getPaint() ); float width = paint.measureText(text, 0, text.length()); return width; } } 

FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.