Câu trả lời được lựa chọn đã mất tích mã, vì vậy ở đây là:
EditText
EditText editText = (EditText) layout.findViewById(R.id.edittext);
editText.setText(msg);
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/myfont.ttf");
editText.setTypeface(font);
RadioButton
RadioButton radioButton = (RadioButton) layout.findViewById(R.id.radiobutton);
radioButton.setText(msg);
Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/myfont.ttf");
radioButton.setTypeface(font);
CheckBox
CheckBox checkBox = (CheckBox) layout.findViewById(R.id.checkbox);
checkBox.setText(msg);
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/myfont.ttf");
checkBox.setTypeface(font);
Nhiều Lần
Nếu bạn cần phải làm điều này cho nhiều quan điểm trên ứng dụng của bạn, sau đó nó có thể được dễ dàng hơn để tạo ra một lớp con của EditText
, RadioButton
của bạn, hoặc CheckBox
. Phân lớp này đặt phông chữ. Dưới đây là ví dụ cho CheckBox
.
public class MyCheckBox extends CheckBox {
// Constructors
public MyCheckBox(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public MyCheckBox(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyCheckBox(Context context) {
super(context);
init();
}
// This class requires myfont.ttf to be in the assets/fonts folder
private void init() {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
"fonts/myfont.ttf");
setTypeface(tf);
}
}
Nó có thể được sử dụng trong xml như sau:
<com.example.projectname.MyCheckBox
android:id="@+id/checkbox"
android:text="@string/msg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="true"/>
tôi đoán, bạn phải sử dụng cùng mã cho các quan điểm khác nữa. Bạn đã thử sử dụng mã để EditText vv. ?? – noob
Có bạn phải sử dụng cùng một mã cho edittext quá. – AndoAiron