【问题标题】:How to disable button with multiple edit text using Textwatcher?如何使用 Textwatcher 禁用带有多个编辑文本的按钮?
【发布时间】:2019-09-21 13:40:45
【问题描述】:

如果我的输入编辑文本为空,我正在尝试禁用我的按钮。我正在为此使用文本观察器。为了测试它,我只尝试了两个编辑文本开始。 但是,无论如何,我的按钮都会保持启用状态。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_create_profile);

    fnameInput = findViewById(R.id.et_firstname);
    lnameInput = findViewById(R.id.et_lastname);
    numberInput = findViewById(R.id.et_phone);
    emailInput = findViewById(R.id.et_email);
    nextBtn = findViewById(R.id.btn_next);

    fnameInput.addTextChangedListener(loginTextWatcher);
    lnameInput.addTextChangedListener(loginTextWatcher);


    nextBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            launchNextActivity();
        }
    });
}

文本观察方法

private TextWatcher loginTextWatcher = 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) {

        String firstNameInput =firstNameInput.getText().toString().trim();
        String lastNameInput = lastNameInput.getText().toString().trim();


        // tried doing it this way 
        nextBtn.setEnabled(!firstNameInput.isEmpty() && !lastNameInput.isEmpty());

        //What i've also tried 
        if(firstNameInput.length()> 0 &&
                lastNameInput.length()>0){
            nextBtn.setEnabled(true);
        } else{
            nextBtn.setEnabled(false);
        }
    }

    @Override
    public void afterTextChanged(Editable s) {

    }
};

我希望在一个或所有输入为空时禁用该按钮,并在填写所有输入字段后启用该按钮。

【问题讨论】:

    标签: java android android-edittext textwatcher


    【解决方案1】:

    创建一个方法检查那里的所有条件

    private void checkTheConditions(){
      if(condition1 && condition2)
      nextBtn.setEnabled(true)
      else
      nextBtn.setEnabled(false)
    }
    

    afterTextChanged(Editable s)方法调用这个方法

    【讨论】:

    • 我将 button.setEnabled(false) 设置为 on、onCreate 以在第一次查看时禁用我的按钮并更改了一些逻辑。但除此之外,当放置在 TextChanged 之后,条件仍然有效。
    • 好的,我写这个答案时要记住一些事情,比如验证用户名和检查密码强度。如果您有多个 textWatcher 并且您正在检查不同的不同条件,那么此答案将对您有所帮助
    【解决方案2】:

    我们现在只考虑 2 个 EditTexts 的情况。 定义2个全局CharSequence如下

    CharSequence fName="";
    CharSequence lName="";
    Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_create_profile);
    
            fnameInput = findViewById(R.id.et_firstname);
            lnameInput = findViewById(R.id.et_lastname);
            numberInput = findViewById(R.id.et_phone);
            emailInput = findViewById(R.id.et_email);
            nextBtn = findViewById(R.id.btn_next);
    
            fnameInput.addTextChangedListener(textWatcher);
            lnameInput.addTextChangedListener(textWatcher2);
    
    
            nextBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    launchNextActivity();
                }
            });
        }
    

    那么你必须为每个 Edittext 定义不同的 textwatcher

    然后在每个 textWatcher 中为上面定义的 CharSequence 赋值

    private TextWatcher textWatcher = 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) {
    
          fName=s;
          validate();  //method to enable or disable button (find method below)
    
        }
    
        @Override
        public void afterTextChanged(Editable s) {
    
        }
    };
    

    现在是 textWatcher2

    private TextWatcher textWatcher2 = 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) {
    
          lName=s;
          validate();  //method to enable or disable button (find method below)
    
        }
    
        @Override
        public void afterTextChanged(Editable s) {
    
        }
    };
    

    现在编写验证方法

    void validate(){
            if (fName.length()>0 && lName.length()>0){
                nextBtn.setEnabled(true);
    
            }else {
                nextBtn.setEnabled(false);
    
            }
        }
    

    【讨论】:

      【解决方案3】:

      哦!你犯了一个小错误。使用OR 条件而不是AND。所以你的代码应该是

      nextBtn.setEnabled(!firstNameInput.isEmpty() || !lastNameInput.isEmpty());
      

      只有当您手动更改EditText 的输入时,TextWatcher 才会发出通知。所以 TextWatcher 不会在启动时闪烁。因此,首先在onCreate 方法中,您应该手动检查那些EditText 字段。

      编辑: Android 新的 DataBinding 库最适合此目的。

      【讨论】:

      • 请通过Android数据绑定库和LiveData。您不需要编写这些数量的样板代码。
      猜你喜欢
      • 2019-01-08
      • 1970-01-01
      • 2020-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多