首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在单个TextUtils中添加2个变量

在单个TextUtils中添加2个变量
EN

Stack Overflow用户
提问于 2020-09-28 23:06:20
回答 1查看 17关注 0票数 0

我有这个问题,我有两个变量- 'mobile_input‘和'mobile_input_login’。我也有两个TextUtils。

我想做一个TextUtils,而不是做两个不同的TextUtils。我在网上搜索过,但没有这样的相关问题。

代码:

代码语言:javascript
复制
mobile_input.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            if (TextUtils.isEmpty(s.toString().trim())) {
                clear2.setVisibility(View.INVISIBLE);

            } else {
                clear2.setVisibility(View.VISIBLE);
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
});


mobile_input_login.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            if (TextUtils.isEmpty(s.toString().trim())) {
                clear4.setVisibility(View.INVISIBLE);
            } else {
                clear4.setVisibility(View.VISIBLE);
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
});

感谢您提前给我答复。

EN

回答 1

Stack Overflow用户

发布于 2020-09-28 23:14:19

您可以创建一个实现TextWatcher的自定义类

代码语言:javascript
复制
public class MyTextWatcher implements TextWatcher {

    private View view;

    public MyTextWatcher(View view) {
        this.view = view;
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

        if (TextUtils.isEmpty(s.toString().trim())) {
            view.setVisibility(View.INVISIBLE);
        } else {
            view.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void afterTextChanged(Editable s) {

    }
});

然后在两个文本字段上使用它:

代码语言:javascript
复制
mobile_input.addTextChangedListener(new MyTextWatcher(clear2));
mobile_input_login.addTextChangedListener(new MyTextWatcher(clear4));

我希望这回答了你的问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64104806

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档