Android 输入法详解

我的另一个博客地址:https://my.oschina.net/hailongqiu/blog/1535494

在这里插入图片描述
在这里插入图片描述

onCreateInputView(创建键盘区视图)
onCreateCand
onStartInputView

在这里插入图片描述

在这里插入图片描述

避免输入法变成全屏
在这里插入图片描述

@Override
    public boolean onEvaluateFullscreenMode() {
        return false;
    }

设置后为 false后
在这里插入图片描述

如何再onStartInputView切换对应的布局
EditText 的 android:inputType 来 如何切换输入法不同的键盘?

在Android 电视上的 搜狗TV输入法,就没有处理密码相关的,导致的BUG就是,在输入WIFI密码或者其它密码的等情况下,中/英 还可以切换,这里问题就很严重.

	public void setXXXXXXXXXX(EditorInfo editorInfo) {
		int inputType = editorInfo.inputType & EditorInfo.TYPE_MASK_CLASS;
		switch (inputType) {
		case EditorInfo.TYPE_CLASS_NUMBER: // 数字键盘
		case EditorInfo.TYPE_CLASS_DATETIME: // 电话号码.
		case EditorInfo.TYPE_CLASS_PHONE: // 日期.
			break;
		case EditorInfo.TYPE_CLASS_TEXT:
		    int v = editorInfo.inputType & EditorInfo.TYPE_MASK_VARIATION;
			if (v == EditorInfo.TYPE_TEXT_VARIATION_EMAIL_ADDRESS
					|| v == EditorInfo.TYPE_TEXT_VARIATION_PASSWORD
					|| v == EditorInfo.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
					|| v == EditorInfo.TYPE_TEXT_VARIATION_URI) {
				// 英文
			} else if (v == EditorInfo.TYPE_TEXT_VARIATION_SHORT_MESSAGE) {
				// 
			}
			break;
		default:
			break;
		}
	}

** 如何去处理我们自定义的按键codes**

public View onCreateInputView() {
        mInputView = (KeyboardView) getLayoutInflater().inflate(R.layout.main, null);
        mInputView.setPreviewEnabled(false);
        mInputView.setOnKeyboardActionListener(this);
        return mInputView;
}

public void onKey(int primaryCode, int[] keyCodes) {
	if (primaryCode == 2250) {
		// .... ..
	}
}

根据回车的状态更新对应的图标以及文字
android:imeOptions 来显示 下一步,完成,搜索 等等?
android:imeActionLabel 如何显示文字在输入法的完成键上的?

 /**
     * 根据回车状态.
     */
    private void updateDoneState() {
        EditorInfo editorInfo = getCurrentInputEditorInfo();
        int action = editorInfo.imeOptions & (EditorInfo.IME_MASK_ACTION | EditorInfo.IME_FLAG_NO_ENTER_ACTION);
        OPENLOG.D("updateDoneState action:" + action);
        switch (action) {
            case EditorInfo.IME_ACTION_GO:
                break;
            case EditorInfo.IME_ACTION_SEARCH: // 搜索
                break;
            case EditorInfo.IME_ACTION_SEND: // 发送
                break;
            case EditorInfo.IME_ACTION_NEXT: // 下一个
                int f = editorInfo.inputType & EditorInfo.TYPE_MASK_FLAGS;
                if (!isCenterMultiLine(editorInfo)) { //  TOGGLE_ENTER_NEXT
                } else { // TOGGLE_ENTER_MULTI_LINE_DONE
                }
                break;
            case EditorInfo.IME_ACTION_DONE:
                if (!isCenterMultiLine(editorInfo)) { // TOGGLE_ENTER_DONE
                } else { // TOGGLE_ENTER_MULTI_LINE_DONE
                }
            default: // 暂时定为多行. TOGGLE_ENTER_MULTI_LINE_DONE
                break;
        }
    }

 /**
     * 判断是否为多行文本 true 多行 false 反之
     */
    private boolean isCenterMultiLine(EditorInfo editorInfo) {
        int f = editorInfo.inputType & EditorInfo.TYPE_MASK_FLAGS;
        return (f == EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE);
    }
    

发送字符串到编辑框

 /**
     * 发送字符到编辑框(EditText)
     */
    public void commitResultText(String resultText) {
        OPENLOG.D("commitResultText resultText:" + resultText);
        InputConnection ic = getCurrentInputConnection();
        if (null != ic && !TextUtils.isEmpty(resultText)) {
            ic.commitText(resultText, 1);
        }
    }

发送回车,空格
sendKeyChar

如何实现删除的
这里需要注意,如果为中文,并且已经输入了一些字母,存在中文选择,优先删除中文.

// KeyEvent.KEYCODE_DEL
private void keyDownUp(int keyEventCode) {
        if (null != getCurrentInputConnection()) {
            getCurrentInputConnection().sendKeyEvent(
                    new KeyEvent(KeyEvent.ACTION_DOWN, keyEventCode));
            getCurrentInputConnection().sendKeyEvent(
                    new KeyEvent(KeyEvent.ACTION_UP, keyEventCode));
        }
    }

输入法如何移动光标的
在这里插入图片描述

 /**
     * 输入框的光标向右移动.
     */
    public void setCursorRightMove() {
        int cursorPos = getSelectionStart();
        cursorPos++;
        if (null != getCurrentInputConnection()) {
            getCurrentInputConnection().setSelection(cursorPos, cursorPos);
        }
    }

    /**
     * 输入框的光标向左移动.
     */
    public void setCursorLeftMove() {
        int cursorPos = getSelectionStart();
        cursorPos--;
        if (cursorPos < 0)
            cursorPos = 0;
        if (null != getCurrentInputConnection()) {
            getCurrentInputConnection().setSelection(cursorPos, cursorPos);
        }
    }

    private static final int MAX_INT = Integer.MAX_VALUE / 2 - 1;

    private int getSelectionStart() {
        if (null != getCurrentInputConnection()) {
            return getCurrentInputConnection().getTextBeforeCursor(MAX_INT, 0).length();
        }
        return 0;
    }

如何去发送表情
比如微信,QQ 等软件,是如何将表情发送出去的.
微信的表情发送有两种,一种是发送某个特定的字符串,比如 “/:basketb”,这个是一个篮球的表情. 第二种 就是发表情图片.

接入语音输入文字
我们已经知道,文本是可以单独去发送,那么这里就很简单啦,只需要将我们说的话,转换成文本,然后使用 commitText 发送这些文本就OK了.

如何使用拼音,注音,倉頡 等字库
其实这里很简单,只需要将相关的字母或者编码 传给 引擎,它就会返回 汉字的列表,然后显示在候选框就可以了.
在这里插入图片描述

安装好厚如何跳转
在这里插入图片描述

还没有写完,需要时间慢慢更新… …

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值