首页 > 解决方案 > 当从editText发送触摸事件时自定义键盘设置可见性消失

问题描述

我想在处理触摸事件时模仿 android 键盘行为。每次我单击编辑文本和键盘以外的其他位置时,android 键盘总是隐藏。到目前为止,我可以模仿这种行为使用dispatchTouchEvent

因为它甚至会检测到任何调度触摸,所以每当我点击我的键盘按钮时,它都会自行关闭键盘。我想检查我是否单击了键盘以外的其他位置,然后隐藏键盘,否则保持键盘打开。我想我只是错过了一段代码,我找不到我在这里错过的东西。它只需要一个步骤即可完成。请帮忙。

这里我的键盘看起来像

自定义键盘

到目前为止我做了什么

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    if (getCurrentFocus() != null) {
        View view = getCurrentFocus();
        //im not sure this condition would help
        if (view == null) {
            keyboard.setVisibility(View.GONE);
        }
    }
    return super.dispatchTouchEvent(ev);
}

我可以获得视图对象,但我无法确定我需要保持键盘打开的视图。

其他代码(变量声明)

PhoneKeyboard pk;
InputConnection ic;
LinearLayout keyboard;

pk = findViewById(R.id.phone_keyboard);
ic = phone_no.onCreateInputConnection(new EditorInfo());
pk.setInputConnection(ic);

处理后按(模仿 android 键盘行为)

@Override
public void onBackPressed(){
    if(keyboard.getVisibility() == View.GONE)
        super.onBackPressed();
    else
        keyboard.setVisibility(View.GONE);
}

我如何处理 editText 上的请求以使我的键盘可见

phone_no.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            InputMethodManager imm = (InputMethodManager) getApplicationContext().getSystemService(
                    android.content.Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

            if(view.equals(phone_no) && keyboard.getVisibility() == View.GONE) {
                phone_no.requestFocus();
                keyboard.setVisibility(View.VISIBLE);
            }
            return false;
        }
    });

我如何在我的自定义键盘上处理提交/输入事件(对于需要自定义键盘解决方案的人,也许这篇文章可以帮助其他人:))

phone_no.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            InputMethodManager imm = (InputMethodManager) getApplicationContext().getSystemService(
                    android.content.Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

            if(view.equals(phone_no) && keyboard.getVisibility() == View.GONE) {
                phone_no.requestFocus();
                keyboard.setVisibility(View.VISIBLE);
            }
            return false;
        }
    });

这是我的键盘布局视图的代码片段

    <include
            layout="@layout/layout_keyboard_phone_no"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:focusableInTouchMode="false"
            app:layout_constraintBottom_toBottomOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

附言。我也有基本活动,它扩展到我的所有活动(如果您的解决方案需要基本活动)

更新 1

我试过这个方法
 phone_no.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (!hasFocus) {
                keyboard.setVisibility(View.GONE);
            }
        }
    });

对 onDispatch 事件做同样的事情,每当我点击自定义键盘上的数字时,它也会隐藏键盘。我需要检查除编辑文本和我的自定义键盘之外的视图,然后隐藏键盘,否则保持键盘打开。谢谢

标签: androidkeyboard

解决方案


推荐阅读