android自定义视图从另一个自定义视图重写其文本

问题描述

我创建了一个只包含三个编辑视图和文本视图的自定义视图

在我的底部表单对话框片段中,我使用了两次不同的 ID,

当第一次显示正常,但在第二次调用时,显示一个视图填充第二个 (即使在另一个片段中使用并按下后退按钮,我也看到了这一点) first time that display ok first time with duplicated value

自定义复合 xml 文件:edit_text_view

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/margin"
    android:orientation="vertical"
    tools:viewBindingIgnore="false">

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/TextInputLayout"
        style="@style/EditText.OutlinedBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:hint="@tools:sample/cities">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/EditText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </com.google.android.material.textfield.TextInputLayout>

    <TextView
        android:id="@+id/TextViewMessage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="start"
        android:textColor="@color/design_default_color_error"
        android:textDirection="anyRtl"
        android:visibility="gone"
        tools:text="@tools:sample/lorem"
        tools:visibility="visible" />
    </LinearLayout>

自定义复合java文件

public class EdittextoutlinedBox extends LinearLayout {
    private EditTextUtils editTextUtils;
    private EditTextViewBinding mBinding;

    public EdittextoutlinedBox(Context context) {
        super(context);
        initializeViews(context,null);
    }

    public EdittextoutlinedBox(Context context,AttributeSet attrs) {
        super(context,attrs);
        initializeViews(context,attrs);
    }

    public void requestEditTextFocus() {
        mBinding.EditText.requestFocus();
    }

    public EdittextoutlinedBox(Context context,AttributeSet attrs,int defStyleAttr) {
        super(context,attrs,defStyleAttr);
        initializeViews(context,attrs);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public EdittextoutlinedBox(Context context,int defStyleAttr,int defStyleRes) {
        super(context,defStyleAttr,defStyleRes);
        initializeViews(context,attrs);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
//        editText = mBinding.EditText;
    }

    public void setGravity(int gravity) {
        mBinding.EditText.setGravity(gravity);
    }

    private void initializeViews(Context context,AttributeSet attrs) {
        if (!isInEditMode()) {

            editTextUtils = new EditTextUtils();
            mBinding = EditTextViewBinding.inflate(LayoutInflater.from(context),this,true);

            if (attrs != null) {
                @SuppressLint("CustomViewStyleable") TypedArray attributes = getContext().obtainStyledAttributes(attrs,R.styleable.ViewCustomFont);
                int[] set = {
                        android.R.attr.background,// idx 0
                        android.R.attr.text,// idx 1
                        android.R.attr.hint,// idx 2
                        android.R.attr.inputType        // idx 3
                };
                TypedArray styledAttributes = context.obtainStyledAttributes(attrs,set);

                try {
                    //region text and hint
                    CharSequence text = styledAttributes.getText((int) 1);
                    if (ValidationUtils.Companion.isNotEmpty(text)) {
                        text = text.toString();
                        setText(text);
                    }
                    CharSequence hint = styledAttributes.getText((int) 2);
                    if (ValidationUtils.Companion.isNotEmpty(hint)) {
                        hint = hint.toString();
                        setHint(hint);
                    }

                    //endregion

                    //region font
                    int font = attributes.getInteger(R.styleable.ViewCustomFont_fonts,0);

                    if (font == Typeface.IRANIAN_SANS_norMAL.getValue()) {
                        mBinding.EditText.setTypeface(new TypefaceProvider().getIranianSans(context));
                    } else if (font == Typeface.IRANIAN_SANS_BOLD.getValue()) {
                        mBinding.EditText.setTypeface(new TypefaceProvider().getIranianSansBold(context));
                    }
                    //endregion

                    //region inputType
                    int inputType = styledAttributes.getInt((int) 3,EditorInfo.TYPE_NULL);
                    if (inputType != EditorInfo.TYPE_NULL) {
                        mBinding.EditText.setInputType(inputType);
                    }
                    //endregion
                } finally {
                    attributes.recycle();
                    styledAttributes.recycle();
                }
            }
        }
    }

    public EditText getEditTextView() {
        return mBinding.EditText;
    }

    public String getText() {
        return editTextUtils.getText(mBinding.EditText);
    }

    public void setText(CharSequence text) {
        mBinding.EditText.setText(text);
    }

    public void setInputTypeAsDigit() {
        editTextUtils.setInputTypeAsDigit(mBinding.EditText);
        mBinding.EditText.setKeyListener(DigitsKeyListener.getInstance("0123456789,"));
        addTextChangedListener(new TextWatcherUtils().separateThousands(getContext(),mBinding.EditText,","."));
    }

    public void addTextChangedListener(TextWatcher watcher) {
        editTextUtils.addTextChangedListener(mBinding.EditText,watcher);
    }

    public void setInputTypeAsPassword() {
        editTextUtils.setInputTypeAsPassword(mBinding.EditText);
    }

    public void setInputTypeAsNumberPassword() {
        editTextUtils.setInputTypeAsNumberPassword(mBinding.EditText);
    }

    public void setInputType(int inputType) {
        editTextUtils.setInputType(mBinding.EditText,inputType);
    }

    public CharSequence getHint() {
        return mBinding.TextInputLayout.getHint();
    }

    public void setHint(CharSequence hint) {
        mBinding.TextInputLayout.setHint(hint);
    }

    public void setMaxLength(int maxLength) {
        editTextUtils.setMaxLength(mBinding.EditText,maxLength);
    }

    public void setMaxLines(int maxLines) {
        editTextUtils.setMaxLines(mBinding.EditText,maxLines);
    }

    public void setError(String errorMessage) {
        editTextUtils.setError(mBinding.EditText,errorMessage);
        requestEditTextFocus();
    }

    public CharSequence getError() {
        return editTextUtils.getError(mBinding.EditText);
    }

    public boolean hasError() {
        return editTextUtils.hasError(mBinding.EditText);
    }

    public void setAsrequired() {
        editTextUtils.setAsrequired(getContext(),mBinding.EditText);
    }
    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)