簡體   English   中英

android中Edittext的初始空間驗證

[英]Initial space validation for Edittext in android

當用戶輸入第一個字符作為空格(即“”)時,如何設置EditText的驗證。 但是我想在所有其他情況下使用空間。 只有第一個字符不能為空格。 如果用戶輸入空格,則我將禁用“發送”按鈕,否則將啟用“發送”按鈕。

你能建議一下嗎。 我嘗試使用TextWatcher的onTextChanged方法。 但是它沒有按預期工作。

謝謝。

請嘗試以下代碼:

mUiEditText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            if (charSequence.toString().startsWith(" ")) {
                //disableButton(...)
            } else {
                //enableButton(...)
            }

        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    });

它應該與TextWatcher一起使用,您是否嘗試過類似的方法:

input.addTextChangedListener(new TextWatcher() {

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

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

    @Override
    public void afterTextChanged(Editable s) {
        String text = s.toString();
        if(text.charAt(0) == ' ')
            button.setClickable(false);
        else
            button.setClickable(true);
    }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM