如何在视图中捕获软键盘输入?

我有一个子类化View,当它在onTouchEvent中收到“触摸”时会弹出键盘。它通过请求焦点,检索InputMethodManager,然后调用showSoftInput来显示此内容。


现在,我需要弄清楚如何在按下软键时捕获它们。我目前仅在软键盘上按下“下一步/完成”按钮时才得到响应。


这是我的课:


public class BigGrid extends View {


    private static final String TAG = "BigGrid";


    public BigGrid(Context context) {

        super(context);

        setFocusableInTouchMode(true); // allows the keyboard to pop up on

                                       // touch down


        setOnKeyListener(new OnKeyListener() {

            public boolean onKey(View v, int keyCode, KeyEvent event) {

                Log.d(TAG, "onKeyListener");

                if (event.getAction() == KeyEvent.ACTION_DOWN) {

                    // Perform action on key press

                    Log.d(TAG, "ACTION_DOWN");

                    return true;

                }

                return false;

            }

        });

    }


    @Override

    public boolean onTouchEvent(MotionEvent event) {

        super.onTouchEvent(event);

        Log.d(TAG, "onTOUCH");

        if (event.getAction() == MotionEvent.ACTION_UP) {


            // show the keyboard so we can enter text

            InputMethodManager imm = (InputMethodManager) getContext()

                    .getSystemService(Context.INPUT_METHOD_SERVICE);

            imm.showSoftInput(this, InputMethodManager.SHOW_FORCED);

        }

        return true;

    }


    @Override

    public InputConnection onCreateInputConnection(EditorInfo outAttrs) {

        Log.d(TAG, "onCreateInputConnection");


        BaseInputConnection fic = new BaseInputConnection(this, true);

        outAttrs.actionLabel = null;

        outAttrs.inputType = InputType.TYPE_CLASS_TEXT;

        outAttrs.imeOptions = EditorInfo.IME_ACTION_NEXT;

        return fic;

    }


    @Override

    public boolean onCheckIsTextEditor() {

        Log.d(TAG, "onCheckIsTextEditor");

        return true;

    }


键盘显示了,但是仅当我按下键盘上的“下一步”按钮时,我的onKeyListener才会触发。我需要点击哪个字符,以便可以在onDraw()方法中显示它。


拉莫斯之舞
浏览 482回答 3
3回答

杨魅力

事实证明,实际上我确实需要继承TextView并使用addTextChangedListener()添加我自己的TextWatcher实现,以便侦听软键事件。我找不到用普通的旧视图执行此操作的方法。另一件事,对于那些将尝试这种技术的人;TextView默认情况下无法编辑文本,因此,如果要使实现可编辑(而不是子类化EditText,而我不想这样做),则还必须创建一个自定义InputConnection,如下所示: /** * MyInputConnection * BaseInputConnection configured to be editable */class MyInputConnection extends BaseInputConnection {    private SpannableStringBuilder _editable;    TextView _textView;    public MyInputConnection(View targetView, boolean fullEditor) {        super(targetView, fullEditor);        _textView = (TextView) targetView;    }    public Editable getEditable() {        if (_editable == null) {            _editable = (SpannableStringBuilder) Editable.Factory.getInstance()            .newEditable("Placeholder");        }        return _editable;    }    public boolean commitText(CharSequence text, int newCursorPosition) {        _editable.append(text);        _textView.setText(text);        return true;    }}然后,使用类似以下内容的方法覆盖onCheckisTextEditor和onCreateInputConnection: @Override public InputConnection onCreateInputConnection(EditorInfo outAttrs) {     outAttrs.actionLabel = null;     outAttrs.label = "Test text";     outAttrs.inputType = InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS;     outAttrs.imeOptions = EditorInfo.IME_ACTION_DONE;     return new MyInputConnection(this, true); } @Override public boolean onCheckIsTextEditor() {     return true; }之后,您应该拥有一个可以收听软键盘的视图,并且可以使用按键输入值执行任何操作。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android