EditText힌트 크기 를 줄이는 방법은 무엇입니까?
EditText힌트 크기 를 줄이는 방법은 무엇입니까?
답변:
문자열 회수에서 크기를 설정하여 할 수 있습니다.
예를 들면 :
<string name="edittext_hint"><font size="15">Hint here!</font></string>
그런 다음 XML에서
android:hint="@string/edittext_hint"
이것은 힌트의 경우 더 작은 텍스트로 재 탐색되지만 입력 텍스트의 경우 원래 크기입니다.
이것이 미래의 독자들에게 도움이되기를 바랍니다.
sp차원 을 지정하는 방법은 size무엇입니까?
의 글꼴 크기를 줄일 수 있습니다. 그러면 EditText의 크기도 줄어 듭니다 hint. 즉android:textSize="16sp"
내 힌트가 표준 크기의 EditText에 맞지 않아서이 작업을 수행해야했습니다. 그래서 나는 이것을했다 (xml에서 textSize를 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);
}
}
});
TextWatcher전화를 걸 수 있습니다 editText.setTextSize(TypedValue.COMPLEX_UNIT_SP, mRealTextSize);. 그러나 longhairedsi의 솔루션은 미래에 더 안전 할 수 있습니다. 이 문제가 최신 SDK 버전에서 수정 될지 궁금합니다.
힌트 문자열 자체에 간단한 HTML 속성을 설정할 수 있습니다.
여기에서 허용되는 답변보기 : Android EditText 힌트
편집하다 : 그냥 직접 가지고 놀았는데, 이것은 나를 위해 일했습니다.
view.setHint(Html.fromHtml("<small><small><small>" +
getString(R.string.hint) + "</small></small></small>"));
다음은 fromHtml에서 허용하는 태그 목록입니다. http://commonsware.com/blog/Android/2010/05/26/html-tags-supported-by-textview.html (하지만 저에게는 작동하지 않았습니다)
프로그래밍 방식으로 수행하려면
SpannableString span = new SpannableString(strHint);
span.setSpan(new RelativeSizeSpan(0.5f), 0, strHint.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
editText.setHint(span);
힌트보다 실제 텍스트의 크기를 더 크게 설정해야합니다.
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;
}
}
힌트의 크기뿐만 아니라 글꼴과 스타일도 변경할 수 있습니다. 나는 그것을 사용하여 해결 SpannableString하고MetricAffectingSpan
1) 사용자 지정 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) 사용자 지정 MetricAffectingSpan개체 만들기 :
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) 사용 :
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);
힌트 글꼴 크기를 변경하기 위해 onFocusChanged () 리스너를 사용하는 것도 옵션입니다. 사용자가 텍스트 필드를 클릭 할 때 addTextChangeListener ()가 트리거되지 않고 깜박이는 커서가 힌트 글꼴로 크기가 조정되기 때문입니다.
또한 TextChangeListener와 달리 초기 힌트 글꼴 크기를 별도로 설정할 필요가 없습니다.
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)
}
}
}
}