美文网首页Android控件
Android动态替换View

Android动态替换View

作者: 一只笔 | 来源:发表于2018-07-27 22:09 被阅读237次

Android动态替换View

在android 在开发中有这样的需求,某一个view需要更换,如果逐步更换效率太低容易出错,下面提供一个动态更换的办法:
把 EditText 动态更换成 自的定的EditText

xml 中的布局

<android.support.constraint.ConstraintLayout 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="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity2"
    tools:showIn="@layout/activity_main">

    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

替换方法

使用自定义的LayoutInflater,便于动态替换自定义的view
判断是否是EditText,如果是替换成 指定view

 /** 使用自定义的LayoutInflater,便于动态替换自定义的view */
    private void installViewFactory() {
        LayoutInflaterCompat.setFactory2(LayoutInflater.from(this), new LayoutInflater.Factory2() {
            @Override
            public View onCreateView(String name, Context context, AttributeSet attrs) {
                return onCreateView(null, name, context, attrs);
            }
            
            @Override
            public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
                // 判断是否是EditText,如果是替换成KeyEventInterceptorEditText
                if (name.equals("EditText") || name.equals("android.support.v7.widget.AppCompatEditText")) {
                    return new KeyEventInterceptorEditText(context, attrs);
                }
                //appcompat 创建view代码
                AppCompatDelegate delegate = getDelegate();
                return delegate.createView(parent, name, context, attrs);
            }
        });
    }

使用方法

在Activity onCreate() super 语句前调用

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        installViewFactory();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

要替换的对象

这里只是监听输入文本的变化,进行打印

  public class KeyEventInterceptorEditText extends AppCompatEditText {
    
    public KeyEventInterceptorEditText(Context context) {
        super(context);
        setListener();
    }
    
    public KeyEventInterceptorEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        setListener();
    }
    
    public KeyEventInterceptorEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setListener();
    }
    
    private void setListener() {
        addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                Log.i("test", "beforeTextChanged");
            }
            
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Log.i("test", "onTextChanged");
            }
            
            @Override
            public void afterTextChanged(Editable s) {
                Log.i("test", "afterTextChanged");
            }
        });
    }
    
    @Override
    public boolean dispatchKeyEventPreIme(KeyEvent event) {
        super.dispatchKeyEventPreIme(event);
        return true;
    }
}

源码下载地址:点击查看

相关文章

网友评论

    本文标题:Android动态替换View

    本文链接:https://www.haomeiwen.com/subject/nwqnmftx.html