【问题标题】:How to determine the current IME in Android?如何确定Android中的当前时间?
【发布时间】:2011-02-14 05:48:46
【问题描述】:

我有一个应用程序,如果用户没有使用默认的 Android 软键盘,我想警告他们。 (即他们正在使用 Swype 或其他东西)。

如何查看他们当前选择了哪种输入法?

【问题讨论】:

    标签: android keyboard ime android-input-method


    【解决方案1】:

    你可以得到一个默认的输入法,使用:

    Settings.Secure.getString(getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
    

    【讨论】:

      【解决方案2】:

      InputMethodManagergetEnabledInputMethodList()。你在Activity 中从getSystemService() 获得InputMethodManager

      【讨论】:

      • 我看到了,它返回了一个可供用户使用的输入法列表——但我似乎不知道如何确定当前选择了哪一个。
      • 不幸的是,我现在唯一能想到的就是检查 getEnabledInputMethodList 返回的列表的长度,如果 >2,警告他们可能有问题,如果他们没有使用默认输入法。任何人有任何其他的指针/想法?
      • 您可能会考虑针对您认为在使用 Swype 的用户中会遇到的问题提出一个新的 SO 问题,并找出解决方法。毕竟,我怀疑随着 Android 进入其他市场(例如电视/机顶盒),“默认 IME”的范围将会扩大。因此,我真的建议我们弄清楚如何获取它,这样您就不必关心他们使用的是什么 IME。
      • 实际上 - 我解决了我在 EditText 上使用 swype 和 TextWatcher 时遇到的问题但是,我仍然需要确定它们是否使用默认键盘。 (它适用于进入速度很重要的游戏,您可以使用 swype 类型的键盘“作弊”。
      • 你在乎他们是否作弊吗?除非你在玩多人游戏,否则我不明白它为什么重要。
      【解决方案3】:

      这是我用来确定是否使用 GoogleKeyboard、Samsung 键盘或 Swype 键盘的一些代码。 mCurId 反射返回的值表示 IME ID。

      使用您正在寻找的不同键盘/输入法进行测试以找到相关的

      public boolean usingSamsungKeyboard(Context context){
          return usingKeyboard(context, "com.sec.android.inputmethod/.SamsungKeypad");
      }
      
      public boolean usingSwypeKeyboard(Context context){
          return usingKeyboard(context, "com.nuance.swype.input/.IME");
      }
      
      public boolean usingGoogleKeyboard(Context context){
          return usingKeyboard(context, "com.google.android.inputmethod.latin/com.android.inputmethod.latin.LatinIME");
      }   
      
      public boolean usingKeyboard(Context context, String keyboardId)
          {
              final InputMethodManager richImm =
                (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
      
              boolean isKeyboard = false;
      
              final Field field;
              try
              {
                  field = richImm.getClass().getDeclaredField("mCurId");
                  field.setAccessible(true);
                  Object value = field.get(richImm);
                  isKeyboard = value.equals(keyboardId);
      
              }
              catch (IllegalAccessException e)
              {
      
              }
              catch (NoSuchFieldException e)
              {
      
              }
              return  isKeyboard;
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-07-10
        • 2019-02-17
        • 2016-12-23
        • 1970-01-01
        • 2013-04-18
        • 2017-02-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多