安卓自定义View接收键盘输入-InputConnection的API

InputConnection接口的实现的基类,负责提供与Editable的连接的大多数常见行为。此类的实现者将希望确保实现 getEditable()提供对自己的可编辑对象的访问,并参考中的文档InputConnection。

int CURSOR_UPDATE_IMMEDIATE InputMethodManager#updateCursorAnchorInfo(android.view.View, CursorAnchorInfo)无论光标/锚位置如何变化,都要求编辑人员尽快调用 。
int CURSOR_UPDATE_MONITOR InputMethodManager#updateCursorAnchorInfo(android.view.View, CursorAnchorInfo) 每当更改光标/锚位置时,要求编辑器调用 。
int GET_EXTRACTED_TEXT_MONITOR 用于标记的标记,用于getExtractedText(ExtractedTextRequest, int)指示您希望在提取的文本发生更改时接收更新。
int GET_TEXT_WITH_STYLES 与一起使用的标志getTextAfterCursor(int, int),getTextBeforeCursor(int, int)并 getSurroundingText(int, int, int)与文字一起返回样式信息。
int INPUT_CONTENT_GRANT_READ_URI_PERMISSION 使用此标志时,编辑器将能够请求对InputContentInfo对象中包含的内容URI的读取访问权。

Public方法

  • beginBatchEdit在API级别3中添加
      public boolean beginBatchEdit ()
      默认实现不执行任何操作。
    
    return
    boolean 如果正在进行批处理编辑,则为true,否则为false。由于此方法开始批量编辑,因此,除非输入连接不再有效,否则它将始终返回true。
  • clearMetaKeyStates

    在API级别3中添加

      public boolean clearMetaKeyStates (int states)
      默认实现用于 MetaKeyKeyListener.clearMetaKeyState(long, int)清除状态。
    
    参数
    states int:要清除的状态,可以是一位或多位。KeyEvent.getMetaState()
    return
    boolean 成功时为true,如果输入连接不再有效,则为false。
  • closeConnection在API级别24中添加
      public void closeConnection ()
      默认实现调用finishComposingText()和 setImeConsumesInput(false)。
      如果重写此方法,则必须调用超类实现。
    
  • commitCompletion在API级别3中添加
      public boolean commitCompletion (CompletionInfo text)
      默认实现不执行任何操作,并返回false。
    
    参数
    text CompletionInfo:已提交的完成。
    return
    boolean 成功时为true,如果输入连接不再有效,则为false。
    • commitContent在API级别25中添加

      public boolean commitContent (InputContentInfo inputContentInfo,
      int flags,
      Bundle opts)
      默认实现,View#performReceiveContent如果allows插入了视图内容,则在目标视图上调用;否则返回false,没有任何副作用。

    参数
    inputContentInfo InputContentInfo:要插入的内容。此值不能为null。
    flags int:如果内容提供者允许或如果应用程序不需要调用 。InputConnection.INPUT_CONTENT_GRANT_READ_URI_PERMISSIONgrantUriPermissions0InputContentInfo#requestPermission()
    opts Bundle:可选的捆绑包数据。这可以null。此值可能是null。
    return
    boolean true如果该请求被应用程序接受,则该请求是否已被处理或仍在后台处理false。
  • commitCorrection在API级别11中添加
      public boolean commitCorrection (CorrectionInfo correctionInfo)
      默认实现不执行任何操作,并返回false。
    
    参数
    correctionInfo CorrectionInfo:有关更正的详细信息。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Android 中,自定义键盘需要实现一个继承自 InputMethodService 的服务,这个服务会在用户打开软键盘时被调用。下面是一些步骤来创建自定义键盘: 1. 创建一个新的 Android 项目,并在 AndroidManifest.xml 文件中声明一个新的服务: ```xml <service android:name=".CustomKeyboard" android:label="Custom Keyboard" android:permission="android.permission.BIND_INPUT_METHOD"> <meta-data android:name="android.view.im" android:resource="@xml/method" /> </service> ``` 上面的代码声明了一个名为 CustomKeyboard 的服务,并将其与 android.view.im 绑定。在 res/xml 目录下创建一个名为 method.xml 的文件,用于指定 CustomKeyboard 的布局和行为: ```xml <?xml version="1.0" encoding="utf-8"?> <input-method xmlns:android="http://schemas.android.com/apk/res/android" android:settingsActivity=".SettingsActivity" android:imeSubtypeLocale="en_US" android:imeSubtypeMode="keyboard" > </input-method> ``` 上面的代码指定了键盘的设置活动、语言环境和子类型模式。 2. 创建 CustomKeyboard 类,并继承 InputMethodService。在这个类中,你需要重写一些回调方法,例如 onCreateInputView()、onKeyDown() 和 onStartInputView() 等。这些方法将决定键盘的外观和行为。 ```java public class CustomKeyboard extends InputMethodService implements KeyboardView.OnKeyboardActionListener { private KeyboardView keyboardView; private Keyboard keyboard; @Override public View onCreateInputView() { keyboardView = (KeyboardView) getLayoutInflater().inflate(R.layout.keyboard, null); keyboard = new Keyboard(this, R.xml.qwerty); keyboardView.setKeyboard(keyboard); keyboardView.setOnKeyboardActionListener(this); return keyboardView; } @Override public void onStartInputView(EditorInfo info, boolean restarting) { super.onStartInputView(info, restarting); keyboardView.setPreviewEnabled(false); } @Override public void onKey(int primaryCode, int[] keyCodes) { InputConnection ic = getCurrentInputConnection(); switch (primaryCode) { case Keyboard.KEYCODE_DELETE: ic.deleteSurroundingText(1, 0); break; case Keyboard.KEYCODE_SHIFT: // do something break; default: char c = (char) primaryCode; ic.commitText(String.valueOf(c), 1); } } } ``` 上面的代码创建了一个名为 CustomKeyboard 的类,并在 onCreateInputView() 方法中设置了键盘的布局和行为。在 onStartInputView() 方法中,我们禁用了键盘预览功能。在 onKey() 方法中,我们检查按下的键码并执行相应的操作。 3. 创建键盘布局。在 res/xml 目录下创建一个名为 qwerty.xml 的文件,用于指定键盘布局: ```xml <?xml version="1.0" encoding="utf-8"?> <Keyboard xmlns:android="http://schemas.android.com/apk/res/android" android:keyWidth="10%p" android:keyHeight="60dp" android:horizontalGap="0px" android:verticalGap="0px" android:keyEdgeFlags="left"> <Row> <Key android:keyLabel="q" android:keyEdgeFlags="left"/> <Key android:keyLabel="w"/> <Key android:keyLabel="e"/> <Key android:keyLabel="r"/> <Key android:keyLabel="t"/> <Key android:keyLabel="y"/> <Key android:keyLabel="u"/> <Key android:keyLabel="i"/> <Key android:keyLabel="o"/> <Key android:keyLabel="p" android:keyEdgeFlags="right"/> </Row> <Row> <Key android:keyLabel="a" android:keyEdgeFlags="left"/> <Key android:keyLabel="s"/> <Key android:keyLabel="d"/> <Key android:keyLabel="f"/> <Key android:keyLabel="g"/> <Key android:keyLabel="h"/> <Key android:keyLabel="j"/> <Key android:keyLabel="k"/> <Key android:keyLabel="l android:keyEdgeFlags="right"/> </Row> <Row> <Key android:keyLabel="shift" android:horizontalGap="10%p" android:keyWidth="20%p" android:keyEdgeFlags="left" android:isModifier="true" android:isSticky="true"/> <Key android:keyLabel="z"/> <Key android:keyLabel=""/> <Key android:keyLabel="c"/> <Key android:keyLabel="v"/> <Key android:keyLabel="b"/> <Key android:keyLabel="n"/> <Key android:keyLabel="m"/> <Key android:keyLabel="delete" android:keyWidth="20%p" android:keyEdgeFlags="right" android:icon="@drawable/ic_delete"/> </Row> <Row> <Key android:keyLabel="123" android:keyEdgeFlags="left" android:keyWidth="20%p"/> <Key android:keyLabel=" " android:keyWidth="40%p"/> <Key android:keyLabel="return" android:keyWidth="20%p" android:keyEdgeFlags="right"/> </Row> </Keyboard> ``` 上面的代码指定了一个基本的 QWERTY 键盘布局,包含字母、数字和删除键。 4. 运行应用程序并测试自定义键盘。在测试键盘时,你需要在 Android 设备的输入法设置中激活你的自定义键盘。 以上就是创建自定义键盘的基本步骤,你可以根据需要修改键盘的布局和行为。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值