2012-04-17 127 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是鍵碼。