美文网首页Android技术知识Android开发经验谈Android开发
[Android 学习笔记] EditView 物理键盘的Ent

[Android 学习笔记] EditView 物理键盘的Ent

作者: afluy | 来源:发表于2019-04-20 16:42 被阅读2次

    EditText 弹出的软键盘显示数字和回车

        <EditText
            android:id="@+id/edit2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:digits="0123456789\n"
            android:inputType="number" />
    

    EditView 监听物理键盘的Enter键和软键盘的回车键, 回传输入的有效字符

    import android.content.Context;
    import android.support.annotation.Nullable;
    import android.text.Editable;
    import android.text.TextUtils;
    import android.text.TextWatcher;
    import android.util.AttributeSet;
    import android.view.KeyEvent;
    import android.widget.TextView;
    import android.widget.Toast;
    
    import static android.view.KeyEvent.KEYCODE_ENTER;
    
    /**
     * 监听物理键盘的Enter键和软键盘的回车键, 回传输入的有效字符
     */
    public class RFEditView extends android.support.v7.widget.AppCompatEditText {
    
        private RFEditViewTextWatcher mTextWatcher = null;
        private TextWatcher mTextWatcherWrapper = 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) {
                if (mTextWatcher != null) {
                    if (s.toString().contains("\n")) {
                        String replace = s.toString().replace("\n", "");
                        setText(replace);
                        setSelection(replace.length());
                        if (!TextUtils.isEmpty(replace)) {
                            mTextWatcher.onInputCompleted(replace);
                        } else {
                            Toast.makeText(getContext(), "输入不能为空", Toast.LENGTH_SHORT).show();
                        }
                    }
                }
            }
    
            @Override
            public void afterTextChanged(Editable s) {
            }
        };
    
        public RFEditView(Context context) {
            super(context);
            init();
        }
    
        public RFEditView(Context context, AttributeSet attrs) {
            super(context, attrs);
            init();
        }
    
        public RFEditView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            init();
        }
    
    
        private void init() {
            addTextChangedListener(mTextWatcherWrapper);
    
            // 监听物理 Enter 键
            setOnEditorActionListener(new OnEditorActionListener() {
                @Override
                public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                    if (event != null && event.getKeyCode() == KEYCODE_ENTER) {
                        if (event.getAction() == KeyEvent.ACTION_DOWN) {
                            if (mTextWatcher != null) {
                                String s = getText().toString();
                                if (TextUtils.isEmpty(s)) {
                                    Toast.makeText(getContext(), "输入不能为空", Toast.LENGTH_SHORT).show();
                                    return false;
                                }
                                String replace = s.replace("\n", "");
                                mTextWatcher.onInputCompleted(replace);
                            }
                        }
                        return true;
                    }
                    return false;
                }
            });
        }
    
        public void setTextWatcher(RFEditViewTextWatcher watcher) {
            mTextWatcher = watcher;
        }
    
        @Nullable
        public String getFormatedText() {
            if (getText() != null) {
                return getTextInput(getText().toString());
            }
            return null;
        }
    
    
        public String getTextInput(String s) {
            if (!TextUtils.isEmpty(s) && s.contains("\n")) {
                String replace = s.replace("\n", "");
                return replace;
            }
            return s;
        }
    
        public interface RFEditViewTextWatcher {
            void onInputCompleted(String s);
        }
    
    }
    
    
    

    相关文章

      网友评论

        本文标题:[Android 学习笔记] EditView 物理键盘的Ent

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