Android 获取InputMethodService中的当前EditText引用
我开发了一个自定义键盘,可以在Android的任何应用程序中使用,按键的压力可以打开一个运行的活动识别 在识别结束时,活动关闭,我需要在打开键盘的EditText(任何EditText,如Sms应用程序)中写入识别的文本,但我不能,因为它可能不再处于活动状态 如何获取打开键盘的活动EditText的引用,然后稍后使用它来编写识别的文本 编辑: 很抱歉代码如下Android 获取InputMethodService中的当前EditText引用,android,mobile-development,Android,Mobile Development,我开发了一个自定义键盘,可以在Android的任何应用程序中使用,按键的压力可以打开一个运行的活动识别 在识别结束时,活动关闭,我需要在打开键盘的EditText(任何EditText,如Sms应用程序)中写入识别的文本,但我不能,因为它可能不再处于活动状态 如何获取打开键盘的活动EditText的引用,然后稍后使用它来编写识别的文本 编辑: 很抱歉代码如下 public class SoftKeyboard extends InputMethodService implements Keybo
public class SoftKeyboard extends InputMethodService implements KeyboardView.OnKeyboardActionListener{
private KeyboardView _kv;
private Keyboard _keyboard;
private InputMethodManager _inputMethodManager;
@Override
public View onCreateInputView() {
_kv = (KeyboardView)getLayoutInflater().inflate(R.layout.keyboard, null);
_kv.setKeyboard(_keyboard);
_kv.setOnKeyboardActionListener(this);
registerReceiver(receiver, new IntentFilter(Const.NOTIFICATION));
return _kv;
}
@Override
public void onKey(int primaryCode, int[] keyCodes) {
InputConnection ic = getCurrentInputConnection();
switch(primaryCode){
case 1010:
// starts View for Barcode scanning
_lastEditorInfo = getCurrentInputEditorInfo();
_lastBarcode = null;
BarcodeActivity mainactivity = new BarcodeActivity();
Intent dialogIntent = new Intent(SoftKeyboard.this, BarcodeActivity.class);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(dialogIntent);
Log.i("Key","Open BarCode Activity");
//ic.commitText(String.valueOf(_lastBarcode),1);
new WaitForBarcode(ic, _inputMethodManager).execute("");
break;
...
}
}
private class WaitForBarcode extends AsyncTask<String, Void, String> {
private InputConnection _icn;
private InputMethodManager _inputMethodManager;
public WaitForBarcode(InputConnection icn, InputMethodManager inputMethodManager) {
_icn = icn;
_inputMethodManager = inputMethodManager;
}
@Override
protected void onPostExecute(String result) {
// might want to change "executed" for the returned string passed
// into onPostExecute() but that is upto you
_inputMethodManager.showSoftInputFromInputMethod(getToken(), InputMethodManager.SHOW_IMPLICIT);
_inputMethodManager.restartInput(_kv);
_kv.setActivated(true);
Log.i("Barcode-last", _lastBarcode);
_icn = getCurrentInputConnection();
_icn.commitText(String.valueOf(_lastBarcode),1);
}
};
公共类软键盘扩展InputMethodService实现KeyboardView.OnKeyboardActionListener{
专用键盘视图_kv;
专用键盘;
私有InputMethodManager\u InputMethodManager;
@凌驾
公共视图onCreateInputView(){
_kv=(KeyboardView)GetLayoutFlater().充气(R.layout.keyboard,null);
_kv.设置键盘(_键盘);
_kv.setOnKeyboardActionListener(本);
registerReceiver(接收方、新意向过滤器(持续通知));
回流_kv;
}
@凌驾
public void onKey(int primaryCode,int[]keyCodes){
InputConnection ic=getCurrentInputConnection();
开关(主代码){
案例1010:
//启动条形码扫描视图
_lastEditorInfo=GetCurrentInputitorInfo();
_lastBarcode=null;
BarcodeActivity mainactivity=新的BarcodeActivity();
Intent dialogIntent=newintent(SoftKeyboard.this、BarcodeActivity.class);
dialogIntent.addFlags(Intent.FLAG\u活动\u新任务);
startActivity(dialogIntent);
Log.i(“键”,“打开条形码活动”);
//ic.commitText(String.valueOf(_lastBarcode),1);
新建WaitForBarcode(ic,_inputMethodManager)。执行(“”);
打破
...
}
}
私有类WaitForBarcode扩展异步任务{
专用输入连接(icn);
私有InputMethodManager\u InputMethodManager;
公共WaitForBarcode(InputConnection icn、InputMethodManager InputMethodManager){
_icn=icn;
_inputMethodManager=inputMethodManager;
}
@凌驾
受保护的void onPostExecute(字符串结果){
//可能需要更改传递的返回字符串的“已执行”
//进入onPostExecute(),但这取决于您
_inputMethodManager.showSoftInputFromInputMethod(getToken(),inputMethodManager.SHOW_隐式);
_inputMethodManager.restartInput(_kv);
_千伏设定值激活(真);
Log.i(“最后一个条形码”,“最后一个条形码”);
_icn=getCurrentInputConnection();
_icn.CommitteText(字符串.valueOf(_lastBarcode),1);
}
};
在onKey()中,我启动了条形码活动,这一项非常有效
在onPostExecute,我在_lastBarcode字段中有正确的条形码,但当我尝试在EditText上发布时,没有条形码(
已解决
在doInBackground上延迟1秒(只是给系统时间来关闭之前的活动)并强制重新打开键盘,CommitteText工作
代码是:
private class WaitForBarcode extends AsyncTask<String, Void, String> {
private InputConnection _icn;
private InputMethodManager _inputMethodManager;
public WaitForBarcode(InputConnection icn, InputMethodManager inputMethodManager) {
_icn = icn;
_inputMethodManager = inputMethodManager;
}
@Override
protected String doInBackground(String... params) {
while (_lastBarcode == null) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.interrupted();
}
}
return _lastBarcode;
}
@Override
protected void onPostExecute(String result) {
// might want to change "executed" for the returned string passed
// into onPostExecute() but that is upto you
_inputMethodManager.showSoftInputFromInputMethod(getToken(), InputMethodManager.SHOW_IMPLICIT);
_inputMethodManager.restartInput(_kv);
getInputView().setActivated(true);
Log.i("Barcode-last", _lastBarcode);
final InputConnection ic = getCurrentInputConnection();
if (ic != null && _lastBarcode != null) {
ic.beginBatchEdit();
ic.commitText(_lastBarcode, 0);
ic.endBatchEdit();
}
}
私有类WaitForBarcode扩展异步任务{
专用输入连接(icn);
私有InputMethodManager\u InputMethodManager;
公共WaitForBarcode(InputConnection icn、InputMethodManager InputMethodManager){
_icn=icn;
_inputMethodManager=inputMethodManager;
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
而(_lastBarcode==null){
试一试{
睡眠(1000);
}捕捉(中断异常e){
Thread.interrupted();
}
}
返回最后一个条形码;
}
@凌驾
受保护的void onPostExecute(字符串结果){
//可能需要更改传递的返回字符串的“已执行”
//进入onPostExecute(),但这取决于您
_inputMethodManager.showSoftInputFromInputMethod(getToken(),inputMethodManager.SHOW_隐式);
_inputMethodManager.restartInput(_kv);
getInputView().setActivated(true);
Log.i(“最后一个条形码”,“最后一个条形码”);
最终输入连接ic=getCurrentInputConnection();
如果(ic!=null&&u lastBarcode!=null){
ic.beginBatchEdit();
ic.CommitteText(_lastBarcode,0);
ic.endBatchEdit();
}
}
如果不查看您的代码,我们将帮不上什么忙。您可以将其保存为静态值?或者可能只是将其发送到另一个活动?@Tirolel yep,该值保存在\u lastBarcode上。但我想将其放在当前关注的编辑文本中。@user8抱歉……您是对的。我用代码修改了请求。您只能获得android.view.InputMethodod、 EditorInfo
,而不是EditText
如果不查看您的代码,我们将无能为力。您可以将其保存为静态值?或者可能只是将其发送到另一个活动?@Tirolel yep,该值保存在“lastBarcode”上。但我想将其放在当前关注的EditText中。@user8抱歉……您是对的。我用代码修改了请求。您可以仅获取android.view.inputmethod.EditorInfo,而不获取EditText