安卓软键盘如何弹出特殊字符?

3
我正在开发Android软键盘,我在字符弹出布局方面遇到了问题。 enter image description here
如上图所示,@是#的弹出字符。如果用户长按#,@就会出现。

我的问题是:当用户点击@字符外部或设备上的返回按钮时,弹出窗口(@)应该消失,但它会一直保留,直到用户点击@为止。
我该怎么做?


你是自己构建的自定义键盘还是系统键盘?在系统键盘中,你无法进行太多更改。如果需要这样的功能,请编写自己的键盘。 - Viral Patel
这是我自己开发的自定义键盘。 - Ehsan Mashhadi
1个回答

0
你只需要覆盖 onTouchEvent 来检测手势并隐藏弹出视图。弹出字符使用在你的键盘布局(XML)中指定的自定义布局,其中包含 android:keyPreviewLayout 标签。
以下是 onTouchEvent 的示例:
@Override
public boolean onTouchEvent(MotionEvent event){

    int action = MotionEventCompat.getActionMasked(event);

    switch(action) {
        case (MotionEvent.ACTION_DOWN) :
            Log.d(DEBUG_TAG,"Action was DOWN");
            return true;
        case (MotionEvent.ACTION_MOVE) :
            Log.d(DEBUG_TAG,"Action was MOVE");
            return true;
        case (MotionEvent.ACTION_UP) :
            Log.d(DEBUG_TAG,"Action was UP");
            return true;
        case (MotionEvent.ACTION_CANCEL) :
            Log.d(DEBUG_TAG,"Action was CANCEL");
            return true;
        case (MotionEvent.ACTION_OUTSIDE) :
            Log.d(DEBUG_TAG,"Movement occurred outside bounds " +
                    "of current screen element");
            return true;
        default :
            return super.onTouchEvent(event);
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接