Jak zmniejszyć EditTextrozmiar podpowiedzi?
Jak zmniejszyć EditTextrozmiar podpowiedzi?
Odpowiedzi:
Możesz to zrobić, ustawiając rozmiar w rekordzie ciągu.
Na przykład:
<string name="edittext_hint"><font size="15">Hint here!</font></string>
następnie w swoim XML po prostu napisz
android:hint="@string/edittext_hint"
Spowoduje to zmianę mniejszego tekstu dla wskazówki, ale oryginalny rozmiar tekstu wejściowego.
Mam nadzieję, że pomoże to przyszłym czytelnikom
spwymiar sizeatrybutu?
Możesz zmniejszyć rozmiar czcionki na EditText- to również zmniejszy rozmiar hint. to znaczyandroid:textSize="16sp"
Musiałem to również zrobić, ponieważ moja wskazówka nie pasowała do tekstu edycji w standardowym rozmiarze. Więc zrobiłem to (w xml ustawiłem textSize na 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);
}
}
});
TextWatchernumer, możesz po prostu zadzwonić editText.setTextSize(TypedValue.COMPLEX_UNIT_SP, mRealTextSize);natychmiast. Jednak rozwiązanie longhairedsi może być bardziej bezpieczne w przyszłości. Jestem ciekaw, czy ten problem zostanie rozwiązany w nowszych wersjach SDK.
Możesz ustawić proste atrybuty HTML w samym ciągu podpowiedzi.
Zobacz zaakceptowaną odpowiedź tutaj: Wskazówka dotycząca edycji tekstu w systemie Android
EDYCJA: po prostu grałem z tym sam, to działało dla mnie:
view.setHint(Html.fromHtml("<small><small><small>" +
getString(R.string.hint) + "</small></small></small>"));
To jest lista tagów akceptowanych przez fromHtml: http://commonsware.com/blog/Android/2010/05/26/html-tags-supported-by-textview.html (chociaż nie działało to dla mnie)
Jeśli chcesz to zrobić programowo,
SpannableString span = new SpannableString(strHint);
span.setSpan(new RelativeSizeSpan(0.5f), 0, strHint.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
editText.setHint(span);
Podejście @marmor jest najlepsze. Możesz zmienić liczbę <small> --- </small>tagów, aby dostosować rozmiar.
Możesz również zdefiniować tekst podpowiedzi bezpośrednio, tak jak ja
view.setHint(Html.fromHtml("<small><small><small>" +
"This is Hint" + "</small></small></small>"));
Mam nadzieję, że to pomoże.
Rozwiązanie @ user2982553 działa świetnie dla mnie. Możesz także użyć AbsoluteSizeSpan, za pomocą którego możesz ustawić dokładny rozmiar czcionki podpowiedzi. Nie używaj <font size=\"5\">tagu, ponieważ sizeatrybut jest po prostu ignorowany.
zdefiniuj to w swoim strings.xml w folderze wartości:
<string name="enter_otp"><font size="16">your text</font></string>
Muszę ustawić większy rozmiar dla prawdziwego tekstu niż podpowiedź.
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;
}
}
Możesz zmienić nie tylko rozmiar podpowiedzi, ale także jej czcionkę i styl. Udało mi się to rozwiązać za pomocą SpannableStringiMetricAffectingSpan
1) Utwórz Hintobiekt niestandardowy :
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) Utwórz MetricAffectingSpanobiekt niestandardowy :
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) zastosowanie:
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);
Użycie detektora onFocusChanged () do zmiany rozmiaru czcionki podpowiedzi jest również opcją, ponieważ addTextChangeListener () nie będzie wyzwalane, gdy użytkownik kliknie pole tekstowe, a migający kursor zmieni rozmiar na czcionkę podpowiedzi.
Ponadto, w przeciwieństwie do TextChangeListener, nie ma potrzeby osobnego ustawiania początkowego rozmiaru czcionki podpowiedzi.
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)
}
}
}
}