ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Android自定义软键盘 – 如何清除编辑文本(提交文本,…)

2019-06-11 22:15:19  阅读:208  来源: 互联网

标签:android android-softkeyboard android-edittext android-textview


我正在关注这个例子:https://android.googlesource.com/platform/development/+/master/samples/SoftKeyboard/

我在添加明文按钮时遇到了麻烦.我想要一个清除焦点文本的按钮.但是由于我重新关注一个非空文本,我不知道如何删除现有的字符.
例如,我有editText A和editText B.

focus A > commit "hello" > focus B > commit "world" > focus A > clear text in A >> FAIL

我仍然可以使用以下方法删除A中逐个字符的文本:

    //keyEventCode = KeyEvent.KEYCODE_DEL
    getCurrentInputConnection().sendKeyEvent(
            new KeyEvent(KeyEvent.ACTION_DOWN, keyEventCode));
    getCurrentInputConnection().sendKeyEvent(
            new KeyEvent(KeyEvent.ACTION_UP, keyEventCode));

但是,由于文本长度未知,因此无法清除文本A.此外,KeyEvent.KEYCODE_CLEAR不适用于上述功能.

任何建议都可能有所帮助,非常感谢.

解决方法:

我做了这样的事情:

InputConnection inputConnection = getCurrentInputConnection();

CharSequence currentText = inputConnection.getExtractedText(new ExtractedTextRequest(), 0).text;
CharSequence beforCursorText = inputConnection.getTextBeforeCursor(currentText.length(), 0);
CharSequence afterCursorText = inputConnection.getTextAfterCursor(currentText.length(), 0);
inputConnection.deleteSurroundingText(beforCursorText.length(), afterCursorText.length());

标签:android,android-softkeyboard,android-edittext,android-textview
来源: https://codeday.me/bug/20190611/1221422.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有