¿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:
- SetWidth () Botón de Android
- Anchos diferentes de diseños de Android en ViewPager
- Cómo establecer el ancho de divisor ListView?
- LinearLayout ImageView Redimensionar todos los anchos de imagen para ajustar
- El elemento ListView no extenderá el ancho a fill_parent
- 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).
- 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.
- No necesito espacio para sugerencia y contenido al mismo tiempo; Las sugerencias desaparecen cuando el usuario comienza a escribir.
- Estoy usando fuentes personalizadas en una clase extendida de
EditText
, pero eso se debe manejar mientras estoy copiando laEditText
deEditText
.
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; } }
- Cómo hacer android buscar barra de ancho completo en api 17 o superior?
- Android layout_width & layout_height, cómo funciona?
- Android DataBinding layout_width: Debe proporcionar un atributo layout_width
- Diseño de android redimensionamiento de los padres sin cambiar el tamaño de los childs
- Diseño de tabla XML? ¿Dos filas de la misma anchura con botones de igual anchura?