2012-04-17 135 views
1

我正在开发Android自定义键盘。我在我的代码中导入了android.view.inputmethod.InputMethodSubtype,同时这样做我收到了一个错误消息,像这样导入的无法解析。是否有任何我需要安装的eclipse插件,根据我的知识Android 1.6以上的版本将支持IMF。Android自定义键盘实现

回答

0

这个问题很古老,但我正在回答它,因为它可能会帮助另一个看到此问题的用户。

OP询问是否有Eclipse的任何插件来安装来解决问题,但现在我们有Android Studio。

对于那些想要实施Android Custom Keyboard: 首先,请下载Android自定义键盘的Google示例项目以开始。

有三个重要的功能来决定你想要哪一个:1)主题(自定义布局),2)子类型和3)表情符号。

对于主题/布局:创建布局文件。请参见下面的示例代码:

<com.domain.keyboard.android.LatinKeyboardView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/keyboard" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:background="@drawable/kb_bg_9" 
    android:keyBackground="@drawable/key_bg_fill_white" 
    android:keyPreviewLayout="@layout/key_preview_layout" 
    android:keyPreviewOffset="@dimen/keyPreviewOffset" 
    android:keyTextColor="@color/white" 
    android:popupLayout="@layout/keyboard_popup_layout" /> 

而且在SoftKeyboard.java使用下面的代码:

@Override 
public View onCreateInputView() { 

    // Set custom theme to input view. 
    int themeLayout = sharedPreferences.getInt(THEME_KEY, R.layout.input_1); 
    mInputView = (LatinKeyboardView) getLayoutInflater().inflate(
      themeLayout, null); 
    mInputView.setOnKeyboardActionListener(this); 

    // Close popup keyboard when screen is touched, if it's showing 
    mInputView.setOnTouchListener(new View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View view, MotionEvent motionEvent) { 
      if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) { 
       mInputView.closing(); 
      } 
      return false; 
     } 
    }); 

    // Apply the selected keyboard to the input view. 
    setLatinKeyboard(getSelectedSubtype()); 

    return mInputView; 
} 

亚型:创建的qwerty.xml副本,并编辑它来代替钥匙。在SoftKeyboard.java中创建LatinKeyboard的另一个实例,并使用ifswitch逻辑。

private LatinKeyboard getSelectedSubtype() { 
    final InputMethodSubtype subtype = mInputMethodManager.getCurrentInputMethodSubtype(); 
    String s = subtype.getLocale(); 
    switch (s) { 
     case "ps_AF": 
      mActiveKeyboard = mPashtoKeyboard; 
      mCurKeyboard = mPashtoKeyboard; 
      break; 
     case "fa_AF": 
      mCurKeyboard = mFarsiKeyboard; 
      break; 
     default: 
      mCurKeyboard = mQwertyKeyboard; 
    } 

    return mCurKeyboard; 
} 

和编辑methods.xml添加亚型:

<input-method xmlns:android="http://schemas.android.com/apk/res/android" 
    android:settingsActivity="com.sunzala.afghankeyboard.android.ImePreferences" 
    android:supportsSwitchingToNextInputMethod="true"> 
    <subtype 
     android:imeSubtypeLocale="en_US" 
     android:imeSubtypeMode="keyboard" 
     android:label="@string/label_subtype_generic" /> 
    <subtype 
     android:imeSubtypeLocale="ps_AF" 
     android:imeSubtypeMode="keyboard" 
     android:label="@string/label_subtype_generic" /> 
    <subtype 
     android:imeSubtypeLocale="fa_AF" 
     android:imeSubtypeMode="keyboard" 
     android:label="@string/label_subtype_generic" /> 
</input-method> 

对于表情:查找库,并将其与键盘相结合。表情符号将按键显示。

if (primaryCode == -10000) { 
    showEmoticons(); 
} 

其中-10000是键码。