美文网首页
Android-EditText默认显示英文键盘且支持自动换行

Android-EditText默认显示英文键盘且支持自动换行

作者: 晖仔Milo | 来源:发表于2024-03-24 11:58 被阅读0次

    1、如题,代码如下

            mBinding.etContent.setInputType(EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE
                    | EditorInfo.TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS);
    

    另附中英文过滤Filter

    public abstract class EditFilterFactory {
    
        abstract InputFilter createInputFilter(EditText editText, int maxWord, String notMatchToast);
    
        public static class SampleEmptyFilter extends EditFilterFactory {
    
            @Override
            InputFilter createInputFilter(EditText editText, int maxWord, String notMatchToast) {
                return new InputFilter() {
                    @Override
                    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                        return source;
                    }
                };
            }
        }
    
        public static class SampleEnFilter extends EditFilterFactory {
    
            @Override
            InputFilter createInputFilter(EditText editText, int maxWord, String notMatchToast) {
                return new InputFilter() {
                    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                        if(TextUtils.isEmpty(source)){
                            return source;
                        }
                        if (maxWord > 0 && editText.getText().toString().split("[ !?.]").length >= maxWord) {
                            ToastUtils.show(String.format("仅支持%s字,已达上限", maxWord));
                            return "";
                        }
    
                        String regex = "[a-zA-Z0-9,.!?' ]+";
                        String unRegex = "[^a-zA-Z0-9,.!?' ]+";
    
                        if (source.toString().matches(regex)) {
                            return source;
                        } else {
                            if (!TextUtils.isEmpty(notMatchToast)) {
                                ToastUtils.show(notMatchToast);
                            }
                            return source.toString().replaceAll(unRegex, "");
                        }
                    }
                };
            }
    
        }
    
        public static class SampleCnFilter extends EditFilterFactory {
    
            @Override
            InputFilter createInputFilter(EditText editText, int maxWord, String notMatchToast) {
                return new InputFilter() {
                    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                        if(TextUtils.isEmpty(source)){
                            return source;
                        }
                        if (maxWord > 0 && editText.getText().toString().length() >= maxWord) {
                            ToastUtils.show(String.format("仅支持%s字,已达上限", maxWord));
                            return "";
                        }
    
                        String regex = "[\u4e00-\u9fa5,。!? ]+";
                        String unRegex = "[^\u4e00-\u9fa5,。!? ]";
    
                        if (source.toString().matches(regex)) {
                            return source;
                        } else {
                            if (!TextUtils.isEmpty(notMatchToast)) {
                                ToastUtils.show(notMatchToast);
                            }
                            return source.toString().replaceAll(unRegex, "");
                        }
                    }
                };
            }
    
        }
    
    }
    

    相关文章

      网友评论

          本文标题:Android-EditText默认显示英文键盘且支持自动换行

          本文链接:https://www.haomeiwen.com/subject/qnsltjtx.html