Làm thế nào để giảm EditTextkích thước Gợi ý?
Làm thế nào để giảm EditTextkích thước Gợi ý?
Câu trả lời:
Bạn có thể làm điều đó bằng cách đặt kích thước trong nguồn cung cấp lại chuỗi.
Ví dụ:
<string name="edittext_hint"><font size="15">Hint here!</font></string>
thì trong XML của bạn chỉ cần viết
android:hint="@string/edittext_hint"
Điều này sẽ kích hoạt lại trong một văn bản nhỏ hơn cho gợi ý nhưng kích thước ban đầu cho văn bản đầu vào.
Hy vọng điều này sẽ giúp ích cho các độc giả trong tương lai
spnguyên cho sizethuộc tính?
Bạn có thể giảm kích thước phông chữ trên EditText- điều đó cũng sẽ giảm kích thước của phông chữ hint. I Eandroid:textSize="16sp"
Tôi cũng phải làm điều này vì gợi ý của tôi không phù hợp với EditText ở kích thước tiêu chuẩn. Vì vậy, tôi đã làm điều này (trong xml đặt textSize thành 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);
}
}
});
TextWatcherbạn có thể chỉ cần gọi editText.setTextSize(TypedValue.COMPLEX_UNIT_SP, mRealTextSize);ngay lập tức. Tuy nhiên, giải pháp của longhairedsi có thể an toàn hơn trong tương lai. Tôi tò mò liệu vấn đề này có được khắc phục trong các phiên bản SDK mới hơn không.
Bạn có thể đặt các thuộc tính HTML đơn giản cho chính chuỗi gợi ý.
Xem câu trả lời được chấp nhận tại đây: Gợi ý Android EditText
CHỈNH SỬA: chỉ tự chơi với nó, điều này đã làm việc cho tôi:
view.setHint(Html.fromHtml("<small><small><small>" +
getString(R.string.hint) + "</small></small></small>"));
Đây là danh sách các thẻ được chấp nhận bởi fromHtml: http://commonsware.com/blog/Android/2010/05/26/html-tags-supported-by-textview.html (mặc dù không hoạt động với tôi)
Nếu bạn muốn làm điều đó theo chương trình,
SpannableString span = new SpannableString(strHint);
span.setSpan(new RelativeSizeSpan(0.5f), 0, strHint.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
editText.setHint(span);
Phương pháp tiếp cận của @marmor là Phương pháp tốt nhất. Bạn có thể thay đổi số lượng<small> --- </small> thẻ để điều chỉnh kích thước.
Bạn cũng có thể xác định văn bản của Gợi ý trực tiếp như tôi đã làm
view.setHint(Html.fromHtml("<small><small><small>" +
"This is Hint" + "</small></small></small>"));
Hy vọng điều này sẽ giúp ích.
Giải pháp của @ user2982553 phù hợp với tôi. Bạn cũng có thể sử dụng AbsoluteSizeSpan, với đó bạn có thể đặt kích thước phông chữ chính xác của gợi ý. Không sử dụng <font size=\"5\">thẻ, vì sizethuộc tính này chỉ bị bỏ qua.
xác định điều này trong thư mục string.xml trong thư mục giá trị:
<string name="enter_otp"><font size="16">your text</font></string>
Tôi cần đặt kích thước lớn hơn cho văn bản thực so với gợi ý.
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;
}
}
Bạn có thể thay đổi không chỉ kích thước của gợi ý mà còn cả phông chữ và kiểu của nó. Tôi đã đạt được giải quyết bằng cách sử dụng SpannableStringvàMetricAffectingSpan
1) Tạo một Hintđối tượng tùy chỉnh :
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) Tạo MetricAffectingSpanđối tượng tùy chỉnh :
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) Sử dụng:
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);
Sử dụng trình nghe onFocusChanged () để thay đổi kích thước phông chữ gợi ý cũng là một tùy chọn, vì addTextChangeListener () sẽ không kích hoạt khi người dùng nhấp vào trường văn bản và con trỏ nhấp nháy sẽ thay đổi kích thước thành phông chữ gợi ý.
Ngoài ra, không giống như TextChangeListener, không cần đặt kích thước phông chữ gợi ý ban đầu riêng biệt.
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)
}
}
}
}