2010-06-29 18 views
45

Cómo reducir EditText ¿Indicar tamaño?Android EditText Sugerencia Tamaño

+2

Creo que no hay necesidad de cambiar el tamaño de la pista. ¿Por qué quieres hacer eso? – Praveen

+3

Sugerencia El tamaño de la fuente es muy grande ... Por lo tanto, necesito reducir 2sp. – Sasikumar

+0

Tuve el mismo problema. Estamos implementando una aplicación de marcador para que los números marcados puedan ser bastante grandes, mientras que el texto de sugerencia debe ser más pequeño para adaptarse a todo. ¡Gracias a @Jeka por la solución! – Bradford2000

Respuesta

41

Puede reducir el tamaño de fuente en el EditText, lo que reducirá el tamaño del hint también. es decir, android:textSize="16sp"

+0

¡Gracias, me salvaste algunos dolores de cabeza! :) – peterkodermac

+0

¡Me alegro de poder ayudar! :) – inky

+0

Gran respuesta en lugar de todo esto Trabajo alrededor: D – Tony

33

También tuve que hacer esto porque mi pista no encajaba en EditText en el tamaño estándar. Así que hice esto (en el conjunto xml TEXTSIZE a mHintTextSize):

MYEditText.addTextChangedListener(new TextWatcher(){ 

       @Override 
       public void afterTextChanged(Editable arg0) { 
        // TODO Auto-generated method stub 

       } 

       @Override 
       public void beforeTextChanged(CharSequence arg0, int arg1, 
         int arg2, int arg3) { 
        // TODO Auto-generated method stub 

       } 

       @Override 
       public void onTextChanged(CharSequence arg0, int start, int before, 
         int count) { 
        if (arg0.length() == 0) { 
         // No entered text so will show hint 
         editText.setTextSize(TypedValue.COMPLEX_UNIT_SP, mHintTextSize); 
        } else { 
         editText.setTextSize(TypedValue.COMPLEX_UNIT_SP, mRealTextSize); 
        } 
       } 
     }); 
+2

¿Qué es EVSearchTerm? – MGDroid

+2

@EVSearchTerm es su referencia de edittext. nombrando la convención abandonada. – Javanator

+0

Este enfoque funciona. El problema del SDK de Android aquí es que el tamaño de texto de programación se ignora por completo para el tamaño de texto de sugerencia. Por otro lado, esto permite simplificar el enfoque de longhairedsi. En lugar de agregar un 'TextWatcher', simplemente puede llamar' editText.setTextSize (TypedValue.COMPLEX_UNIT_SP, mRealTextSize); 'inmediatamente. Sin embargo, la solución de longhairedsi podría ser más segura en el futuro. Tengo curiosidad si este problema se solucionará en las versiones más recientes de SDK. –

31

Puede establecer los atributos HTML simple a la propia cadena de pista.

Ver respuesta aceptada aquí: Android EditText hint

EDIT: Acabo de jugar con él mismo, esto funcionó para mí:

view.setHint(Html.fromHtml("<small><small><small>" + 
      getString(R.string.hint) + "</small></small></small>")); 

Ésta es una lista de etiquetas aceptadas por fromHtml: http://commonsware.com/blog/Android/2010/05/26/html-tags-supported-by-textview.html (aunque no funcionó para mí)

8

es fácil de reducir el tamaño de sugerencia del texto de edición

editText.setHint(Html.fromHtml(
    "<font size=\"5\">" + "hinttext1" + "</font>" + 
    "<small>" + "hinttext2" + "</small>")); 
1

@marmor 's Approach es el mejor. Puede modificar el número de etiquetas <small> --- </small> para ajustar el tamaño.

También puede definir el texto de sugerencia directamente como lo hice

view.setHint(Html.fromHtml("<small><small><small>" + "This is Hint" + "</small></small></small>"));

Esperamos que esto ayude.

6

Si desea hacerlo mediante programación,

SpannableString span = new SpannableString(strHint); 
span.setSpan(new RelativeSizeSpan(0.5f), 0, strHint.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
editText.setHint(span); 
1

solución user2982553 's @ funciona muy bien para mí. También puede usar AbsoluteSizeSpan, con el cual puede establecer el tamaño de letra exacto de la sugerencia. No use la etiqueta <font size=\"5\">, porque el atributo size simplemente se ignora.

73

Puede hacerlo configurando un tamaño en el recource de cadena.

Por ejemplo:

<string name="edittext_hint"><font size="15">Hint here!</font></string> 

entonces en su XML acaba de escribir

android:hint="@string/edittext_hint" 

Esto resault en un texto más pequeño por la pista, pero el tamaño original para el texto de entrada.

espera que esta ayuda para los futuros lectores

+1

¿Cómo especificar la dimensión '' sp'' para el atributo '' size''? –

+0

funciona como un amuleto –

+1

Como no está especificando la dimesión como sp aquí, ¿no sería un problema en un tamaño de pantalla diferente? No encuentro ninguna forma de establecer el tamaño de sugerencia a través del diseño xml sin causar problemas cuando cambian las dimensiones –

0

que necesitan establecer un tamaño más grande para el texto real que toque.

public static class LargeSizeTextWatcher implements TextWatcher { 

    private final EditText mEditText; 
    private final int mOriginalSize; 
    private final int mLargeSize; 

    private int mLastLength; 

    TrackingNumberTextWatcher(EditText editText) { 
     mEditText = editText; 
     mOriginalSize = (int) editText.getTextSize(); 
     mLargeSize = editText.getResources().getDimensionPixelSize(R.dimen.text_size_large); 

     mLastLength = editText.length(); 
     if (mLastLength != 0) { 
      mEditText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mLargeSize); 
     } 
    } 

    @Override 
    public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
    } 

    @Override 
    public void onTextChanged(CharSequence s, int start, int before, int count) { 
    } 

    @Override 
    public void afterTextChanged(Editable s) { 
     int length = s.length(); 
     if (length == 0) { 
      mEditText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mOriginalSize); 
     } else if (mLastLength == 0) { 
      mEditText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mLargeSize); 
     } 
     mLastLength = length; 
    } 
} 
0

Puede cambiar no solo el tamaño de una pista, sino también su fuente y estilo.He conseguido resolverlo utilizando SpannableString y MetricAffectingSpan

1) Crear un objeto personalizado Hint:

import android.graphics.Typeface; 
import android.text.SpannableString; 
import android.text.Spanned; 
import android.text.style.MetricAffectingSpan; 

public class CustomHint extends SpannableString 
{ 
    public CustomHint(final CharSequence source, final int style) 
    { 
     this(null, source, style, null); 
    } 

    public CustomHint(final CharSequence source, final Float size) 
    { 
     this(null, source, size); 
    } 

    public CustomHint(final CharSequence source, final int style, final Float size) 
    { 
     this(null, source, style, size); 
    } 

    public CustomHint(final Typeface typeface, final CharSequence source, final int style) 
    { 
     this(typeface, source, style, null); 
    } 

    public CustomHint(final Typeface typeface, final CharSequence source, final Float size) 
    { 
     this(typeface, source, null, size); 
    } 

    public CustomHint(final Typeface typeface, final CharSequence source, final Integer style, final Float size) 
    { 
     super(source); 

     MetricAffectingSpan typefaceSpan = new CustomMetricAffectingSpan(typeface, style, size); 
     setSpan(typefaceSpan, 0, source.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE); 
    } 
} 

2) Crear la costumbre MetricAffectingSpan objeto:

import android.graphics.Typeface; 
import android.text.TextPaint; 
import android.text.style.MetricAffectingSpan; 

public class CustomMetricAffectingSpan extends MetricAffectingSpan 
{ 
    private final Typeface _typeface; 
    private final Float _newSize; 
    private final Integer _newStyle; 

    public CustomMetricAffectingSpan(Float size) 
    { 
     this(null, null, size); 
    } 

    public CustomMetricAffectingSpan(Float size, Integer style) 
    { 
     this(null, style, size); 
    } 

    public CustomMetricAffectingSpan(Typeface type, Integer style, Float size) 
    { 
     this._typeface = type; 
     this._newStyle = style; 
     this._newSize = size; 
    } 

    @Override 
    public void updateDrawState(TextPaint ds) 
    { 
     applyNewSize(ds); 
    } 

    @Override 
    public void updateMeasureState(TextPaint paint) 
    { 
     applyNewSize(paint); 
    } 

    private void applyNewSize(TextPaint paint) 
    { 
     if (this._newStyle != null) 
      paint.setTypeface(Typeface.create(this._typeface, this._newStyle)); 
     else 
      paint.setTypeface(this._typeface); 

     if (this._newSize != null) 
      paint.setTextSize(this._newSize); 
    } 
} 

3) Uso :

Typeface newTypeface = Typeface.createFromAsset(getAssets(), "AguafinaScript-Regular.ttf"); 
CustomHint customHint = new CustomHint(newTypeface, "Enter some text", Typeface.BOLD_ITALIC, 60f); 
     //  CustomHint customHint = new CustomHint(newTypeface, "Enter some text", Typeface.BOLD_ITALIC); 
     //  CustomHint customHint = new CustomHint(newTypeface, "Enter some text", 60f); 
     //  CustomHint customHint = new CustomHint("Enter some text", Typeface.BOLD_ITALIC, 60f); 
     //  CustomHint customHint = new CustomHint("Enter some text", Typeface.BOLD_ITALIC); 
     //  CustomHint customHint = new CustomHint("Enter some text", 60f); 

customEditText.setHint(customHint);