Comment réduire EditText
Taille de l'indice ?
Réponses
Trop de publicités?Je dois définir une taille plus grande pour le texte réel que pour l'indice.
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;
}
}
Vous pouvez modifier non seulement la taille d'un indice mais aussi sa police et son style. J'ai réussi à le résoudre en utilisant SpannableString
y MetricAffectingSpan
1) Créez un Hint
objet :
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) Créez des MetricAffectingSpan
objet :
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) Utiliser :
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);
L'utilisation de l'écouteur onFocusChanged() pour modifier la taille de la police d'indication est également une option, car addTextChangeListener() ne se déclenchera pas lorsque l'utilisateur cliquera sur le champ de texte, et le curseur clignotant sera redimensionné à la police d'indication.
En outre, contrairement au TextChangeListener, il n'est pas nécessaire de définir séparément la taille de la police de l'indice initial.
class EditTextWithHintSize {
init {
val typedArray = context.obtainStyledAttributes(attrs,
R.styleable.EditTextWithHintSize, 0, defStyle)
try {
hintFontSize = typedArray.getDimension(R.styleable.EditTextWithHintSize_hint_font_size, textSize)
fontSize = textSize
if (length() == 0) {
setTextSize(TypedValue.COMPLEX_UNIT_PX, hintFontSize)
}
} catch (e: Exception) {
hintFontSize = textSize
fontSize = textSize
} finally {
typedArray.recycle()
}
}
override fun onFocusChanged(focused: Boolean, direction: Int, previouslyFocusedRect: Rect?) {
super.onFocusChanged(focused, direction, previouslyFocusedRect)
if (focused) {
setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize)
} else {
if (length() == 0) {
setTextSize(TypedValue.COMPLEX_UNIT_PX, hintFontSize)
} else {
setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize)
}
}
}
}
- Réponses précédentes
- Plus de réponses