2015-06-10 21 views
16

Ai đó đã cố thay đổi phông chữ của nhãn nổi? Tôi đã thay đổi nguồn gốc của EditText nhưng phông chữ của nhãn nổi không thay đổi, tôi rất biết ơn những người giúp tôiThay đổi phông chữ của nhãn nổi EditText và TextInputLayout

Code:

   <android.support.design.widget.TextInputLayout 
        android:id="@+id/tilTextoDescricao" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:layout_toRightOf="@id/tilValorUnidade" 
        android:layout_marginTop="10dp"> 

        <EditText 
         android:id="@+id/etTextoDescricao" 
         android:layout_width="fill_parent" 
         android:layout_height="wrap_content" 
         android:layout_marginLeft="5dp" 
         android:hint="Descrição" 
         android:textSize="15dp" 
         android:inputType="text" /> 

       </android.support.design.widget.TextInputLayout> 

----------------- 

    etTextoDescricao= (EditText) findViewById(R.id.etTextoDescricao); 
    etTextoDescricao.setTypeface(CustomTypeface.getTypefaceMediumDefault(this)); 

enter image description here

Trả lời

18

Thật không may, bạn sẽ phải để sử dụng sự phản chiếu để xử lý điều này.

Nhãn nổi được vẽ bởi CollapsingTextHelper, là lớp nội bộ, gói riêng tư và không được thiết lập để xử lý các nhịp. Vì vậy, sử dụng một cái gì đó như một tùy chỉnh TypefaceSpan sẽ không hoạt động trong trường hợp này.

Vì điều này sử dụng sự phản chiếu nên không đảm bảo hoạt động trong tương lai.

Thực hiện

final Typeface tf = Typeface.createFromAsset(getAssets(), "your_custom_font.ttf"); 
final TextInputLayout til = (TextInputLayout) findViewById(R.id.yourTextInputLayout); 
til.getEditText().setTypeface(tf); 
try { 
    // Retrieve the CollapsingTextHelper Field 
    final Field cthf = til.getClass().getDeclaredField("mCollapsingTextHelper"); 
    cthf.setAccessible(true); 

    // Retrieve an instance of CollapsingTextHelper and its TextPaint 
    final Object cth = cthf.get(til); 
    final Field tpf = cth.getClass().getDeclaredField("mTextPaint"); 
    tpf.setAccessible(true); 

    // Apply your Typeface to the CollapsingTextHelper TextPaint 
    ((TextPaint) tpf.get(cth)).setTypeface(tf); 
} catch (Exception ignored) { 
    // Nothing to do 
} 

xem Lỗi

Nếu bạn cần thay đổi font chữ của lỗi, bạn có thể làm một trong hai điều:

  1. Sử dụng Reflection lấy lỗi TextView và áp dụng Typeface giống như trước
  2. Sử dụng khoảng tùy chỉnh. Không giống như nhãn nổi, chế độ xem lỗi được sử dụng bởi TextInputLayout chỉ là một TextView, do đó, nó có thể xử lý các nhịp.

Sử dụng phản ánh

final Field errorField = til.getClass().getDeclaredField("mErrorView"); 
errorField.setAccessible(true); 
((TextView) errorField.get(til)).setTypeface(tf); 

Sử dụng một khoảng tùy chỉnh

final SpannableString ss = new SpannableString("Error"); 
ss.setSpan(new FontSpan(tf), 0, ss.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
til.setError(ss); 

private static final class FontSpan extends MetricAffectingSpan { 

    private final Typeface mNewFont; 

    private FontSpan(Typeface newFont) { 
     mNewFont = newFont; 
    } 

    @Override 
    public void updateDrawState(TextPaint ds) { 
     ds.setTypeface(mNewFont); 
    } 

    @Override 
    public void updateMeasureState(TextPaint paint) { 
     paint.setTypeface(mNewFont); 
    } 

} 

Kết quả

results

Phông chữ tôi đang sử dụng là Smoothie Shoppe.

+3

Không đẹp, nhưng siêu hữu ích cho bây giờ, cảm ơn! – StingRay5

+1

Xin Chúa ban phước cho bạn! Lưu tôi –

+0

Làm thế nào tôi có thể sử dụng điều này trong lớp đơn, vấn đề của tôi là tôi có rất nhiều bố cục sau đó tất cả các thời gian tôi phải sử dụng mã này trong tất cả các lớp đó là lý do tại sao tôi đang ask.Is nó có thể? –

5

Dưới đây là triển khai lớp tùy chỉnh cho adneal's answer.

public class CustomTextInputLayout extends TextInputLayout { 

    public CustomTextInputLayout(Context context) { 
     super(context); 
     initFont(context); 
    } 

    public CustomTextInputLayout(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     initFont(context); 
    } 

    private void initFont(Context context) { 
     final Typeface typeface = Typeface.createFromAsset(
       context.getAssets(), "fonts/YOUR_CUSTOM_FONT.ttf"); 

     EditText editText = getEditText(); 
     if (editText != null) { 
      editText.setTypeface(typeface); 
     } 
     try { 
      // Retrieve the CollapsingTextHelper Field 
      final Field cthf = TextInputLayout.class.getDeclaredField("mCollapsingTextHelper"); 
      cthf.setAccessible(true); 

      // Retrieve an instance of CollapsingTextHelper and its TextPaint 
      final Object cth = cthf.get(this); 
      final Field tpf = cth.getClass().getDeclaredField("mTextPaint"); 
      tpf.setAccessible(true); 

      // Apply your Typeface to the CollapsingTextHelper TextPaint 
      ((TextPaint) tpf.get(cth)).setTypeface(typeface); 
     } catch (Exception ignored) { 
      // Nothing to do 
     } 
    } 
} 

Trong file XML của bạn bây giờ bạn cần phải sử dụng CustomTextInputLayout thay vì TextInputLayout và nó sẽ làm việc ra khỏi hộp.

<your.package.CustomTextInputLayout 
    android:id="@+id/textInputLayout_email" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

    <AutoCompleteTextView 
     android:id="@+id/editText_email" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="@string/hint_email" 
     android:inputType="textEmailAddress" /> 

Cảm ơn adneal để có câu trả lời.

24

Kể từ Design Library v23, bạn có thể sử dụng TextInputLayout#setTypeface().

Điều này sẽ đặt kiểu chữ trên cả gợi ý mở rộng và nổi.

Đây là số feature request nơi được thảo luận trên b.android.com.

CHỈNH SỬA: Kiểu chữ chế độ xem lỗi không được đặt nhưng hiện là fixed trong v25.1.0.

+1

Tôi đã làm điều này và nó làm việc cho tất cả mọi thứ nhưng xem lỗi. Tôi nghĩ rằng textappearance ghi đè lên nó, vì vậy đó có thể là một lỗi của một số loại. Để nhắm mục tiêu chế độ xem lỗi cụ thể, bạn có thể sử dụng ID do android cấp để tham chiếu nó như sau: ** ((TextView) inputLayout.findViewById (R.id.textinput_error)). SetTypeface (customFont); ** –

+0

@ JustinLiu Sự cố của bạn đã được khắc phục trong Thư viện hỗ trợ v25.1.0 - https://code.google.com/p/android/issues/detail?id=227803 –

1

khắc phục sự cố trong câu trả lời @adneal: nếu setErrorEnabled không được đặt đúng, mErrorView sẽ là rỗng và nếu bạn đặt sai tại bất kỳ thời điểm nào phông chữ sẽ thay đổi về mặc định. để khắc phục vì:

trong bạn ghi đè tùy chỉnh TextInputLayout setErrorEnabled

@Override 
public void setErrorEnabled(boolean enabled) { 

    super.setErrorEnabled(enabled); 

    if (enabled) { 

     try { 

      Field cthf = TextInputLayout.class.getDeclaredField("mErrorView"); 
      cthf.setAccessible(true); 

      TextView error = (TextView) cthf.get(this); 

      if (error != null) 
       error.setTypeface(tf); 


     } catch (Exception e) { 

     } 
    } 
} 
0
final Typeface tf = Typeface.createFromAsset(getAssets(), "your_custom_font.ttf"); 
final TextInputLayout til = (TextInputLayout) findViewById(R.id.yourTextInputLayout); 
til.getEditText().setTypeface(tf); 
til.setTypeface(tf); 
+0

Vui lòng giải thích điều gì đó về cách tiếp cận của bạn – etalon11

+0

@ etalon11, Văn bản xem hoặc chỉnh sửa văn bản sẽ được bọc bên trong 'TextInputLayout' và áp dụng một kiểu khuôn mặt cho' TextInputLayout' cuối cùng sẽ áp dụng các thuộc tính cho nó. Tôi tin rằng điều này sẽ làm việc. – Satty

+0

Thành viên TextInputLayout mEditText mà getEditText() trả về khác với mErrorView, đây là một TextView. Điều này không hoạt động, vui lòng kiểm tra. –

5

tôi chỉ tìm thấy một giải pháp đơn giản và nó làm việc cho tôi:

theo cách này bạn có thể thiết lập các kiểu chữ để gợi ý bất kỳ văn bản chỉnh sửa nào:

trong layout.xml:

<android.support.design.widget.TextInputLayout 
      android:id="@+id/text_input1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 
      <EditText 
       android:id="@+id/edt_user" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:hint="@string/username"/> 
     </android.support.design.widget.TextInputLayout> 

và trong lớp java:

public class MainActivity extends AppCompatActivity { 

EditText editText; 
TextInputLayout textInputLayout; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Typeface font_yekan= Typeface.createFromAsset(getAssets(), "fonts/byekan.ttf"); 
     textInputLayout= (TextInputLayout) findViewById(R.id.text_input1); 
    textInputLayout.setTypeface(font_yekan); 
     } 
} 
+0

Tuyệt vời. Nó làm việc cho tôi. –

+1

Lạ lùng, điều này đã có tác dụng đối với tôi. Tôi đã sử dụng bố cục nhập văn bản tùy chỉnh và chỉ thay đổi kiểu chữ trong khi khởi tạo bằng cách sử dụng setTypeface() và vẫn là chế độ xem lỗi có kiểu cũ hơn. Điều này là lạ vì nó có trong mã mà xem lỗi cũng có kiểu chữ trong setTypeface(). Bất kỳ đầu mối nào tôi có thể bị thiếu? –

0

Đây là cách tôi đạt được điều này

edit_login_emailOrPhone.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
     @Override 
     public void onFocusChange(View v, boolean hasFocus) { 
      if(hasFocus) 
      { 
       textInputLayout_login_emailOrPhone.setTypeface(APSApplication.getInstance().getFonts().getTypefaceSemiBold()); 
      }else 
      { 
       textInputLayout_login_emailOrPhone.setTypeface(APSApplication.getInstance().getFonts().getTypefaceRegular()); 
      } 
     } 
    }); 
Các vấn đề liên quan