2010-06-05 17 views
4

Je me demande comment est-il possible de faire une clé de forme personnalisée sur le clavier android. Pour créer un clavier personnalisé est relativement facile, mais ce qui doit être fait pour créer de nouveaux boutons. Ils doivent également répondre à tous les événements en tant que clavier normal.Android: Comment créer des touches de forme personnalisées sur le clavier?

Des idées par où commencer?

Répondre

2

Vous devez créer une image pour chaque touche de votre clavier personnalisé, puis créer le fichier XML prenant en charge la conception de votre clavier et le charger dans votre application avec la classe Keyboard. Avez-vous besoin de plus de détails? Si c'est le cas, je vais en préparer un si vous voulez, je voulais faire un essai dans cette classe depuis un moment.

+0

ne suffit pas redéfinissant la fonction createKeyFromXml() de la classe de clavier? – Solata

+0

oh oui, je suppose que c'est possible, même si je n'ai jamais essayé. Je ne vous ai pas bien compris, je pensais que vous vouliez changer tout le clavier – Sephy

+0

Vous avez bien compris, je pensais juste que ce serait plus facile. Je suppose que non. Peut-être que vous devriez aller un peu plus dans les détails :) – Solata

1

charge comme cette

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 classe statique neccessaire car avec l'aide de ce que vous pouvez changer d'état de clé dans xml si vous voulez.

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>