Android知识Android开发Android技术知识

Android监听页面所有TextView 是否变化

2017-02-23  本文已影响785人  旺仔爸
使用场景 页面上有许多输入框 只有当页面信息有变动的时候 提交按钮才可用
这只是一种实现思路  如果有更好欢迎留言

Activity 页面调用

 /**
     * 监听页面所有TextView 是否变化
     */
    private void observeTextViewChange(){

        List<TextView>  textViewList =   ViewTool.getAllChildTextViewFormActivity(context);
        for (final TextView textView:textViewList) {

            textView.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                    viewTitleRightText.setEnabled(true);
                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    viewTitleRightText.setEnabled(true);
                }

                @Override
                public void afterTextChanged(Editable s) {
                    viewTitleRightText.setEnabled(true);
                }
            });
        }
    }

获取页面所有View 的方法 (一下方法可以自行发挥封装 这只是一种思路)

/**
 * @note 获取该activity所有view
 * @author liuh
 * */
public static List<TextView> getAllChildTextViewFormActivity(Context context) {
    View view = ((Activity)context).getWindow().getDecorView();
    return getAllChildViews(view);
}

private static List<TextView> getAllChildViews(View view) {
    List<TextView> allchildren = new ArrayList<>();
    if (view instanceof ViewGroup) {
        ViewGroup vp = (ViewGroup) view;
        for (int i = 0; i < vp.getChildCount(); i++) {
            View viewchild = vp.getChildAt(i);
            if (viewchild instanceof TextView){
                allchildren.add((TextView) viewchild);

            }
            allchildren.addAll(getAllChildViews(viewchild));

        }
    }
    return allchildren;
}
上一篇下一篇

猜你喜欢

热点阅读