从InputConnection获取EditText中所有文本的更好方法? [英] Better way to get all the text in an EditText from an InputConnection?

查看:398
本文介绍了从InputConnection获取EditText中所有文本的更好方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一个IME(InputMethodService),我需要从它正在编辑的EditText中获取所有文本.我知道一种方法:

I've written an IME (InputMethodService) and I need to get all the text from the EditText it is editing. I know one way:

InputConnection inputConnection = getCurrentInputConnection();
inputConnection.append(inputConnection.getTextBeforeCursor(9999, 0))
.append(inputConnection.getTextAfterCursor(9999, 0));

它有效,但是看起来很笨拙.但是,没有这样的方法InputConnection.getText().

It works, but it seems pretty stupid and clunky. However there is no such method InputConnection.getText().

有更好的方法吗?

P.S.我不能从我的IME访问EditText,因为它属于父应用程序,因此,除非您知道获取EditText的方法,否则请不要告诉我使用EditText.getText()!

P.S. I cannot access the EditText from my IME because it belongs to the parent app so please don't tell me to use EditText.getText(), unless you know a way to get the EditText!

推荐答案

这里还有另一种方法:

inputConnection.performContextMenuAction(android.R.id.selectAll);
CharSequence sData =  inputConnection.getSelectedText(0);

这篇关于从InputConnection获取EditText中所有文本的更好方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆