根据两个或多个EditText字段中的输入启用/禁用按钮的优雅方式

时间:2018-04-06 08:44:19

标签: android android-edittext textwatcher android-textwatcher addtextchangedlistener

我读过这个问题 How to disable Button if EditText is empty ?

但它只有1个UwpDesktop字段。如果两个EditText字段都为空或包含文本,则使用TextWatcher启用或禁用Button的优雅解决方案是什么?

这是我的方法并且它有效,但它没有使用EditText中传递的任何参数。你觉得怎么样?

onTextChanged

4 个答案:

答案 0 :(得分:2)

也许你可以像这样为你的editTexts创建一个包装器。

public class MyEditText extends AppCompatEditText implements TextWatcher {

    public interface LoginTextWatcher {
        void onTextChanged(String... texts);
    }

    private static List<MyEditText> myEditTextList = new ArrayList<>();
    private static LoginTextWatcher loginTextWatcherListener;

    public MyEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public MyEditText(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyEditText(Context context) {
        this(context, null, 0);
    }

    public static void setLoginTextWatcherListener(LoginTextWatcher listener) {
       loginTextWatcherListener = listener;
    }

    public void addLoginTextWatcher() {
        super.addTextChangedListener(this);
        myEditTextList.add(this);
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        if (loginTextWatcherListener != null) {
            String[] textArray = new String[myEditTextList.size()];
            for (int index = 0; index < myEditTextList.size(); index++) {
                textArray[index] = myEditTextList.get(index).getText().toString();
            }
            loginTextWatcherListener.onTextChanged(textArray);
        }
    }

    @Override
    public void afterTextChanged(Editable editable) {

    }

然后你就这样使用它,

public class MainActivity extends AppCompatActivity implements LoginTextWatcher {

private MyEditText editTextUsername;
private MyEditText editTextPassword;
private Button buttonConfirm;

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

    editTextUsername = findViewById(R.id.edit_text);
    editTextPassword = findViewById(R.id.edit_password);
    buttonConfirm = findViewById(R.id.button_confirm);

    MyEditText.setLoginTextWatcherListener(this);
    editTextUsername.addLoginTextWatcher();
    editTextPassword.addLoginTextWatcher();
}

@Override
public void onTextChanged(String... texts) {
   //do your work
}

}

我在这里所做的并不是什么大不了的事。你有一个包装器,在那里管理回调并根据需要传递给你想要的活动。我相信你的活动以这种方式看起来更优雅。如果您不希望在每次文本更改后循环,则可以传递editTexts。你知道,你可以随心所欲地改变一切。将方法添加到MyEditText类中以删除这些静态引用并在某些时候调用它们也很好。也许当活动正在破坏时。

答案 1 :(得分:1)

您可以将listeners设置为EditText,如

editTextUsername.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) {
              button.setEnabled(editTextUsername.getText().toString().trim().length() > 0 
                  && editTextPassword .getText().toString().trim().length() > 0 ); 
            }

            @Override
            public void afterTextChanged(Editable s) {

             }
        });

    editTextPassword.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) {
              button.setEnabled(editTextUsername.getText().toString().trim().length() > 0 
                  && editTextPassword .getText().toString().trim().length() > 0 ); 
            }

            @Override
            public void afterTextChanged(Editable s) {

             }
        });

答案 2 :(得分:1)

使用 textwatcher for both edittext

再添加一个条件以检查 anotheredittext.getText().toString().trim().isEmpty()

并使用此代码在textwatcher中启用按钮,

 buttonConfirm.setEnabled(!(s.toString().trim().isEmpty() || anotheredittext.getText().toString().trim().isEmpty()));

答案 3 :(得分:0)

@anzaidemirzoi的答案的科特林版本(同样,对于我正在使用的TextInputEditText):

import android.content.Context
import android.support.design.widget.TextInputEditText
import android.text.Editable
import android.text.TextWatcher
import android.util.AttributeSet

class TextWatcherInputEditText @JvmOverloads
    constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) :
    TextInputEditText(context, attrs, defStyleAttr),
    TextWatcher {

    interface IntegratedTextWatcher {
        fun onTextChanged(texts: String)
    }

    init {
        super.addTextChangedListener(this)
    }
    private var integratedTextWatcherListener: IntegratedTextWatcher? = null

    fun setLoginTextWatcherListener(listener: IntegratedTextWatcher) {
        integratedTextWatcherListener = listener
    }

    override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {

    }

    override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {
        if (integratedTextWatcherListener != null) {
            integratedTextWatcherListener!!.onTextChanged(charSequence.toString())
        }
    }

    override fun afterTextChanged(editable: Editable) {

    }
}