2010-06-05 148 views

回答

2

您需要爲您的自定義鍵盤上的每個鍵創建一個圖像,然後創建支持鍵盤設計的xml,並將其加載到您的應用程序中,並使用Keyboard類。 你需要更多的細節? 如果是這樣,我會準備一個,如果你想,我一直想在這個班上去一段時間。

+0

是不足夠的覆蓋鍵盤類的createKeyFromXml()函數? – Solata 2010-06-05 12:25:58

+0

噢,我想這是可能的,但我從來沒有嘗試過。沒有正確理解你,我以爲你想改變整個鍵盤 – Sephy 2010-06-05 12:43:35

+0

你理解正確,我只是認爲這將是更簡單的方法。我猜不會。也許你應該再詳細一點:) – Solata 2010-06-05 20:53:36

1

裝載方法

Keyboard mQwertyKeyboard = new LatinKeyboard(this, R.xml.qwerty); 

LatinKeyboard

public class LatinKeyboard extends Keyboard 
{ 

    private Key mEnterKey; 
    private Key mSpaceKey; 

    private Key mModeChangeKey; 

    private Key mLanguageSwitchKey; 

    private Key mSavedModeChangeKey; 

    private Key mSavedLanguageSwitchKey; 

    public LatinKeyboard(Context context, int xmlLayoutResId) { 
     super(context, xmlLayoutResId); 
    } 

    public LatinKeyboard(Context context, int layoutTemplateResId, 
      CharSequence characters, int columns, int horizontalPadding) { 
     super(context, layoutTemplateResId, characters, columns, horizontalPadding); 
    } 

    @Override 
    protected Key createKeyFromXml(Resources res, Row parent, int x, int y, 
      XmlResourceParser parser) { 
     Key key = new LatinKey(res, parent, x, y, parser); 
     if (key.codes[0] == 10) { 
      mEnterKey = key; 
     } else if (key.codes[0] == ' ') { 
      mSpaceKey = key; 
     } else if (key.codes[0] == Keyboard.KEYCODE_MODE_CHANGE) { 
      mModeChangeKey = key; 
      mSavedModeChangeKey = new LatinKey(res, parent, x, y, parser); 
     } else if (key.codes[0] == LatinKeyboardView.KEYCODE_LANGUAGE_SWITCH) { 
      mLanguageSwitchKey = key; 
      mSavedLanguageSwitchKey = new LatinKey(res, parent, x, y, parser); 
     } 
     return key; 
    } 


    void setLanguageSwitchKeyVisibility(boolean visible) { 
     if (visible) { 
      // The language switch key should be visible. Restore the size of the mode change key 
      // and language switch key using the saved layout. 
      mModeChangeKey.width = mSavedModeChangeKey.width; 
      mModeChangeKey.x = mSavedModeChangeKey.x; 
      mLanguageSwitchKey.width = mSavedLanguageSwitchKey.width; 
      mLanguageSwitchKey.icon = mSavedLanguageSwitchKey.icon; 
      mLanguageSwitchKey.iconPreview = mSavedLanguageSwitchKey.iconPreview; 
     } else { 
      // The language switch key should be hidden. Change the width of the mode change key 
      // to fill the space of the language key so that the user will not see any strange gap. 
      mModeChangeKey.width = mSavedModeChangeKey.width + mSavedLanguageSwitchKey.width; 
      mLanguageSwitchKey.width = 0; 
      mLanguageSwitchKey.icon = null; 
      mLanguageSwitchKey.iconPreview = null; 
     } 
    } 
    void setImeOptions(Resources res, int options) { 
     if (mEnterKey == null) { 
      return; 
     } 

     switch (options&(EditorInfo.IME_MASK_ACTION|EditorInfo.IME_FLAG_NO_ENTER_ACTION)) { 
      case EditorInfo.IME_ACTION_GO: 
       mEnterKey.iconPreview = null; 
       mEnterKey.icon = null; 
       mEnterKey.label = res.getText(R.string.label_go_key); 
       break; 
      case EditorInfo.IME_ACTION_NEXT: 
       mEnterKey.iconPreview = null; 
       mEnterKey.icon = null; 
       mEnterKey.label = res.getText(R.string.label_next_key); 
       break; 
      case EditorInfo.IME_ACTION_SEARCH: 
       mEnterKey.icon = res.getDrawable(R.drawable.sym_keyboard_search); 
       mEnterKey.label = null; 
       break; 
      case EditorInfo.IME_ACTION_SEND: 
       mEnterKey.iconPreview = null; 
       mEnterKey.icon = null; 
       mEnterKey.label = res.getText(R.string.label_send_key); 
       break; 
      default: 
       mEnterKey.icon = res.getDrawable(R.drawable.sym_keyboard_return); 
       mEnterKey.label = null; 
       break; 
     } 
    } 

    void setSpaceIcon(final Drawable icon) { 
     if (mSpaceKey != null) { 
      mSpaceKey.icon = icon; 
     } 
    } 

    static class LatinKey extends Keyboard.Key { 

     public LatinKey(Resources res, Keyboard.Row parent, int x, int y, 
       XmlResourceParser parser) { 
      super(res, parent, x, y, parser); 
     } 

     /** 
     * Overriding this method so that we can reduce the target area for the key that 
     * closes the keyboard. 
     */ 
     @Override 
     public boolean isInside(int x, int y) { 
      return super.isInside(x, codes[0] == KEYCODE_CANCEL ? y - 10 : y); 
     } 
    } 

} 

靜態類LatinKey是neccessary因爲這樣的幫助,如果你願意,你可以在XML改變鍵的狀態。

qwerty.xml

<?xml version="1.0" encoding="utf-8"?> 

<Keyboard xmlns:android="http://schemas.android.com/apk/res/android" 
    android:keyWidth="10%p" 
    android:horizontalGap="0px" 
    android:verticalGap="0px" 
    android:keyHeight="@dimen/key_height" 
    > 

    <Row> 
     <Key android:codes="113" android:keyLabel="q" android:keyEdgeFlags="left"/> 
     <Key android:codes="119" android:keyLabel="w"/> 
     <Key android:codes="101" android:keyLabel="e"/> 
     <Key android:codes="114" android:keyLabel="r"/> 
     <Key android:codes="116" android:keyLabel="t"/> 
     <Key android:codes="121" android:keyLabel="y"/> 
     <Key android:codes="117" android:keyLabel="u"/> 
     <Key android:codes="105" android:keyLabel="i"/> 
     <Key android:codes="111" android:keyLabel="o"/> 
     <Key android:codes="112" android:keyLabel="p" android:keyEdgeFlags="right"/> 
    </Row> 

    <Row> 
     <Key android:codes="97" android:keyLabel="a" android:horizontalGap="5%p" 
       android:keyEdgeFlags="left"/> 
     <Key android:codes="115" android:keyLabel="s"/> 
     <Key android:codes="100" android:keyLabel="d"/> 
     <Key android:codes="102" android:keyLabel="f"/> 
     <Key android:codes="103" android:keyLabel="g"/> 
     <Key android:codes="104" android:keyLabel="h"/> 
     <Key android:codes="106" android:keyLabel="j"/> 
     <Key android:codes="107" android:keyLabel="k"/> 
     <Key android:codes="108" android:keyLabel="l" android:keyEdgeFlags="right"/> 
    </Row> 

    <Row> 
     <Key android:codes="-1" android:keyIcon="@drawable/sym_keyboard_shift" 
       android:keyWidth="15%p" android:isModifier="true" 
       android:isSticky="true" android:keyEdgeFlags="left"/> 
     <Key android:codes="122" android:keyLabel="z"/> 
     <Key android:codes="120" android:keyLabel="x"/> 
     <Key android:codes="99" android:keyLabel="c"/> 
     <Key android:codes="118" android:keyLabel="v"/> 
     <Key android:codes="98" android:keyLabel="b"/> 
     <Key android:codes="110" android:keyLabel="n"/> 
     <Key android:codes="109" android:keyLabel="m"/> 
     <Key android:codes="-5" android:keyIcon="@drawable/sym_keyboard_delete" 
       android:keyWidth="15%p" android:keyEdgeFlags="right" 
       android:isRepeatable="true"/> 
    </Row> 

    <Row android:rowEdgeFlags="bottom"> 
     <Key android:codes="-3" android:keyIcon="@drawable/sym_keyboard_done" 
       android:keyWidth="15%p" android:keyEdgeFlags="left"/> 
     <Key android:codes="-2" android:keyLabel="123" android:keyWidth="10%p"/> 
     <!-- 
      android:codes: -101 is not a framework-defined key code but a key code that is 
      privately defined in com.example.android.softkeyboard.LatinKeyboardView. 
     --> 
     <Key android:codes="-101" android:keyIcon="@drawable/sym_keyboard_language_switch" 
       android:keyWidth="10%p"/> 
     <Key android:codes="32" android:keyIcon="@drawable/sym_keyboard_space" 
       android:keyWidth="30%p" android:isRepeatable="true"/> 
     <Key android:codes="46,44" android:keyLabel=". ," 
       android:keyWidth="15%p"/> 
     <Key android:codes="10" android:keyIcon="@drawable/sym_keyboard_return" 
       android:keyWidth="20%p" android:keyEdgeFlags="right"/> 
    </Row> 
</Keyboard>