[Android 学习笔记] EditView 物理键盘的Enter键和软键盘的回车键

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);
    }

}


    原文作者:afluy
    原文地址: https://www.jianshu.com/p/ac3f6f9d3408
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞