为TextInputLayout创建自定义类 [英] Create Custom class for TextInputLayout
本文介绍了为TextInputLayout创建自定义类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在TextInputLayout中,我想通过Custom类添加TextInputEditText这是我创建的自定义类的代码:
In TextInputLayout I want to add TextInputEditText via Custom class Here is the code for custom class I have created :
public class CustomTextInputEditText extends TextInputLayout {
private TextInputEditText editText;
public CustomTextInputEditText(@NonNull Context context) {
this(context, null);
}
public CustomTextInputEditText(@NonNull Context context, @Nullable AttributeSet attrs) {
this(context, attrs, R.style.Widget_MaterialComponents_TextInputLayout_OutlinedBox);
}
public CustomTextInputEditText(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs, defStyleAttr);
}
private void init(Context context, AttributeSet attrs, int defStyleAttr) {
removeAllViews();
setWillNotDraw(false);
editText = new TextInputEditText(getContext());
createEditBox(editText);
}
private void createEditBox(TextInputEditText editText) {
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
editText.setPadding(0,10,0,0);
editText.setLayoutParams(layoutParams);
}
}
这是Xml代码:
<com.example.myapplication.CustomTextInputEditText
android:id="@+id/custom_view"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:boxStrokeColor="@color/colorAccent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="parent"/>
它不显示自定义视图的任何预览
It doesn't show any preview of the custom view
推荐答案
public class CustomTextInputEditText extends TextInputLayout {
private TextInputEditText editText;
public CustomTextInputEditText(@NonNull Context context) {
this(context, null);
}
public CustomTextInputEditText(@NonNull Context context, @Nullable AttributeSet attrs) {
this(context, attrs, R.style.Widget_MaterialComponents_TextInputLayout_OutlinedBox);
}
public CustomTextInputEditText(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs, defStyleAttr);
}
private void init(Context context, AttributeSet attrs, int defStyleAttr) {
//removeAllViews();
setWillNotDraw(false);
editText = new TextInputEditText(getContext());
createEditBox(editText);
}
private void createEditBox(TextInputEditText editText) {
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
editText.setPadding(0,10,0,0);
editText.setLayoutParams(layoutParams);
addView(editText);
}
}
这篇关于为TextInputLayout创建自定义类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文