如何防止EditText中单词之间的多个空格?

时间:2018-03-04 22:45:54

标签: android android-edittext

我正在尝试找到一种方法来防止EditText中的单词之间存在多个空格,并且还可以防止前导空格。我尝试用TextWatcher这样做,但我无法弄清楚如何做到这一点。 任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:6)

您可以使用InputFilter执行此操作。 随意使用此代码段:

InputFilter filter = new InputFilter() {
    public CharSequence filter(CharSequence source, int start, int end,
                               Spanned dest, int dstart, int dend) {
        String stringSource = source.toString();
        String stringDest = dest.toString();
        if (stringSource.equals(" ")) {
            if (stringDest.length() == 0)
                return "";
            if (stringDest.length() >= 1)
                if ((dstart > 0 && txt2.charAt(dstart - 1) == ' ') || (txt2.length() >  dstart && txt2.charAt(dstart) == ' ') || dstart == 0)
                    return "";
        }
        return null;
    }
};
editText.setFilters(new InputFilter[]{filter});

以下是我的应用的结果:

enter image description here

希望这有帮助!

答案 1 :(得分:0)

import android.text.InputFilter
import android.text.Spanned


/**don't allow enter "space" at start and multiply times in EditText*/
class StartMultiplySpaceFilter : InputFilter {
    override fun filter(source: CharSequence, start: Int, end: Int, dest: Spanned?, dstart: Int, dend: Int): CharSequence {
        for (i in start until end) {
            if (Character.isWhitespace(source[i]) && dstart == 0) {
                /*don't allow space at start*/
                return ""
            }
        }

        val stringDest = dest?.toString()
        if (source.toString() == " ") {
            if (stringDest.isNullOrEmpty()) {
                return ""
            } else if (dstart > 0 && stringDest[dstart - 1] == ' '
                || stringDest.length > dstart && stringDest[dstart] == ' '
                || dstart == 0
            ) {
                /*don't multiply spaces*/
                return ""
            }
        }
        return source
    }
}

用法:

editText.setFilters(new InputFilter[]{new StartMultiplySpaceFilter()});

答案 2 :(得分:0)

如果您将 EditTextTextChangedListener/ TextWatcher 一起使用,则代码将是:

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

            }

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

                // prevents the space key/character for the first time and multiple space in the middle of the edit text search
                String text = myEditText.getText().toString();
                InputFilter filter = new InputFilter() {
                    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                        String stringSource = source.toString();
                        String stringDest = dest.toString();
                        if (stringSource.equals(" ")) {
                            if (stringDest.length() == 0)
                                return "";
                            if (stringDest.length() >= 1)
                                if ((dstart > 0 && text.charAt(dstart - 1) == ' ') || (text.length() >  dstart && text.charAt(dstart) == ' ') || dstart == 0)
                                    return "";
                        }
                        return null;
                    }
                };
                myEditText.setFilters(new InputFilter[]{filter});


            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });