在InputMethodService中获取当前的EditText引用

时间:2016-06-09 08:16:48

标签: android mobile-development

我开发了一个CustomKeyboard,用于Android的任何应用程序,即一键的压力打开正在运行的Activity识别。

在识别结束时,Activity关闭,我需要在打开键盘的EditText中编写已识别的文本(任何EditText,如Sms App),但我不能,因为它可能不再活动。

如何获取对打开键盘的活动EditText的引用,然后再使用它来编写已识别的文本?

编辑: 对不起......代码在下面

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);
    }

};

在onKey()中我启动了BarcodeActivity,这个工作非常完美。

在onPostExecute中我在_lastBarcode字段中有正确的条形码,但是当我尝试在EditText上发布时,不存在;(

解决 在doInBackground上放置1秒的延迟(只是为了给系统时间关闭以前的Activity)并强制重新打开键盘,commitText工作。

代码是:

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();
        }

    }

0 个答案:

没有答案