Android动态替换View

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;
    }
}

源码下载地址:点击查看

最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 180,971评论 25赞 708
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 7,727评论 0赞 17
  • Day1: 在代码中通过R.string.hello_world可以获得该字符串的引用; 在XML中通过@stri...
    冰凝雪国阅读 1,774评论 0赞 5
  • 天下,分久必合,合久必分,自是不变的定律。 东汉末年分三国,自黄巾起义,到天下一统,在这不长的时间里,涌现了一批怀...
    翊谖阅读 442评论 0赞 0
  • 下雨天,想起你; 看到躲雨的屋檐,想起你; 看到屋檐下的情侣,想起你; 看到情侣相握的手,想起你。 雨停了, 情侣...
    该好友最近没有发春阅读 241评论 0赞 0

友情链接更多精彩内容