【问题标题】:Android can't get token to change soft keyboardAndroid 无法获取更改软键盘的令牌
【发布时间】:2016-02-27 23:10:18
【问题描述】:

我有一个自定义键盘,其中有一个按钮可以更改 IME....

从 Android 示例代码中,我发现了以下内容:

InputMethodManager imeManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
imeManager.switchToNextInputMethod(getToken(), false /* onlyCurrentIme */);

但我只是得到can't resolve 'getToken()'...我正在从一个扩展InputMethodService的类中执行此操作...那么从键盘内部获取令牌的正确方法是什么?

如果不是上述情况,那么这个也可以:

imeManager.showInputMethodPicker();
imeManager.hideSoftInputFromWindow(/*token?*/, 0);

我的班级:

public class CustomKeyboard extends InputMethodService implements KeyboardView.OnKeyboardActionListener {

    private KeyboardView kv;
    private Keyboard keyboard;

    public final static int CodeRowFourGlobe = 55041;

    @Override
    public View onCreateInputView() {
        kv = (KeyboardView)getLayoutInflater().inflate(R.layout.keyboard, null);
        keyboard = new Keyboard(this, R.xml.custom_keyboard);
        kv.setPreviewEnabled(false);
        kv.setKeyboard(keyboard);
        kv.setOnKeyboardActionListener(this);
        return kv;
    }

    public void closeKeyboard(View view) {
        InputMethodManager imeManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
        imeManager.switchToNextInputMethod(view.getWindowToken(), false /* onlyCurrentIme */);
    }

    @Override
    public void onPress(int primaryCode) {

    }

    @Override
    public void onRelease(int primaryCode) {

    }

    @Override
    public void onKey(int primaryCode, int[] keyCodes) {
        InputConnection ic = getCurrentInputConnection();

        if (primaryCode == Keyboard.KEYCODE_DELETE) {
            ic.deleteSurroundingText(1, 0);
        } else if (primaryCode == CodeRowFourGlobe) {
            InputMethodManager imeManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
            if (imeManager != null) {
                Log.d(TAG, "Close keyboard");

            } else {
                Toast.makeText(this, R.string.no_keyboard_available, Toast.LENGTH_LONG).show();
            }
        } else {

        }
    }
}

我试过了:

InputMethodManager imm =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(kv.getApplicationWindowToken(), 0);
imm.switchToNextInputMethod(kv.getApplicationWindowToken(), false);
imm.switchToNextInputMethod(kv.getWindowToken(), false /* onlyCurrentIme */);
imm.hideSoftInputFromWindow(kv.getWindowToken(), 0);

【问题讨论】:

    标签: android ime


    【解决方案1】:

    这对我有用:

    IBinder token = getWindow().getWindow().getAttributes().token;
    

    可以在InputMethodService 内的任何地方使用。

    【讨论】:

      【解决方案2】:

      所以我的诀窍是使用以下内容:

      private IBinder getToken() {
          final Dialog dialog = getWindow();
          if (dialog == null) {
              return null;
          }
          final Window window = dialog.getWindow();
          if (window == null) {
              return null;
          }
          return window.getAttributes().token;
      }
      

      【讨论】:

        【解决方案3】:

        你可以试试这样的

        public void fun(View view) {
               InputMethodManager imeManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
               imeManager.switchToNextInputMethod(view.getWindowToken(), false /* onlyCurrentIme */);
        }
        

        【讨论】:

        • 好的,但是由于它是从键盘而不是从视图中调用的,那么我应该使用哪个视图来关闭它?使用KeyboardView好像没有效果
        • closeKeyboard(kv);(kv 是我的 KeyboardView 和 closeKeyboard,而不是你的代码中的乐趣)
        • 你需要通过当前视图。
        • 但这是我的问题...我是从 KeyboardView 调用它,而不是从应用程序内部调用它...那么我如何获得正确的活动视图?
        • 那拿活动当前的重点试试?
        猜你喜欢
        • 2015-03-18
        • 1970-01-01
        • 1970-01-01
        • 2016-11-10
        • 1970-01-01
        • 2013-02-02
        • 2016-05-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多