模拟软键盘删除操作
第一种方式:
123456789101112131415161718192021222324 /*** 删除表情和文字*/private void deleteEmoji(String str, int selection) {int start = -1, end = -1;Matcher matcher = emoji.matcher(str);while (matcher.find()) { // 遍历最后一个String key = matcher.group();start = matcher.start();end = matcher.start() + key.length();}if (start != -1 && end != -1) { // 删除表情messageContent.getText().delete(start, end);} else {if (selection > 0) { // 删除文字messageContent.getText().delete(selection - 1, selection);}}}// 表情规则private Pattern emoji = Pattern.compile("[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]", Pattern.UNICODE_CASE| Pattern.CASE_INSENSITIVE);第二种方式:
123456789101112131415 /*** 模拟删除按键*/private void deleteKey() {AsyncTask.execute(new Runnable() {@Overridepublic void run() {try {Instrumentation inst = new Instrumentation();inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DEL);} catch (Exception ignored) {}}});}