gpt4 book ai didi

android - 为 TextInputLayout 创建自定义类

转载 作者:行者123 更新时间:2023-12-04 15:49:45 25 4
gpt4 key购买 nike

在 TextInputLayout 我想通过自定义类添加 TextInputEditText
这是我创建的自定义类的代码:

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"/>

它不显示自定义 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);
}
}

关于android - 为 TextInputLayout 创建自定义类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59249129/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com