2011-10-23 17 views
3

私は自分のIMEをデバイス用に作成しています。追加する必要があるのは、下の図のようにkeyboardViewの上にあるTextBoxです。下の図のように表示できますが、テキストを書き込めません。EditText on InputKeyboard launchアンドロイドでIMEを作成する

enter image description here

私は

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/wrapper" 
    android:layout_height="wrap_content" android:orientation="vertical" 
    android:layout_width="fill_parent" android:background="@color/background" > 

    <TextView android:textAppearance="?android:attr/textAppearanceMedium" android:layout_height="wrap_content" android:id="@+id/txtTest" android:layout_width="fill_parent" android:text="Test" ></TextView> 
     <EditText android:inputType="text" android:id="@+id/edtTest" android:layout_height="wrap_content" android:layout_width="fill_parent"></EditText> 

<com.keyboard.CustomKeyboardView 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/keyboard" 
      android:layout_alignParentBottom="true" 
      android:layout_width="fill_parent" android:layout_height="wrap_content" 
      android:keyTextSize="15sp" 
     /> 
    </LinearLayout> 

パブリッククラスCustomKeyboardViewがKeyboardView {

static final int KEYCODE_OPTIONS = -100; 

private TextView mResultText; 
public CustomKeyboardView (Context context, AttributeSet attrs) { 
    super(context, attrs); 
    } 

public CustomKeyboardView(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
} 

@Override 
protected boolean onLongPress(Key key) { 
    if (key.codes[0] == Keyboard.KEYCODE_CANCEL) { 
     getOnKeyboardActionListener().onKey(KEYCODE_OPTIONS, null); 
     return true; 
    } else { 
     return super.onLongPress(key); 
    } 
} 

おかげで、 ナシ

+0

少なくともあなたのレイアウトを提供できますか? KeyboardViewを拡張していますか? –

+0

@Laurentがコードで更新されましたレイアウトに何か追加する必要がありますか? – nilMoBile

+0

これは、あなたのCustomKeyboardViewレイアウトではなく、あなたのActivityレイアウトだと思います。 CustomKeyboardViewレイアウト(またはonCreate())を提供できますか? –

答えて

2

することができますを拡張レイアウト構造をキーボードビューを拡張し、以下でいます捕獲あなたのソフトキーボードイベントを受け取り、KeyboardView.OnKeyboardActionListenerを実装して自分のウィジェットに送信します。あなたのInputmethodservice.onKey()方法で

、あなたはこのようなあなたのInputViewのサブビューにイベントを送信しようとする必要があります:

public class mySoftKeyboard 
    extends InputMethodService 
    implements KeyboardView.OnKeyboardActionListener { 

// Implementation of KeyboardViewListener inside your InputMethodService 
public void onKey(int primaryCode, int[] keyCodes) { 
     //assuming your inputview is in private variable mInputView 
     //and contains public members txtTst and edtTst views 
     //(arrange for this in your InputView.onCreate) 
     //Here, we just transmit the onKey code to View.onKeyDown/Up and let views draw themselves 
     sendKey(mInputView.txtTst , primaryCode); // send this to your TextView 
     sendKey(mInputView.edtTst , primaryCode); // also send to your EditText 
    } 

/** 
* Helper to send a character to the editor as raw key events. 
*/ 
private void sendKey(View v, int keyCode) { 
      v.onKeyDown(keyCode,new KeyEvent(KeyEvent.ACTION_DOWN, keyCode)); 
      v.onKeyUp (keyCode,new KeyEvent(KeyEvent.ACTION_UP, keyCode)); 
} 
    //other interface function, no need to implement 
public void onText(CharSequence text){} 
public void swipeRight() {} 
public void swipeLeft() {} 
public void swipeDown() {} 
public void swipeUp() {} 
public void onPress(int primaryCode) {} 
public void onRelease(int primaryCode) {} 
} 

編集

をグリフとの違いについてあなたのコメントに答えるためにキーコードは、あなたに役立つコードスニペットです:

//This snippet tries to translate the glyph 'primaryCode' into key events 

//retrieve the keycharacter map from a keyEvent (build yourself a default event if needed) 
KeyCharacterMap myMap=KeyCharacterMap.load(event.getDeviceId()); 

//event list to reproduce glyph 
KeyEvent evs[]=null; 

//put the primariCode into an array 
char chars[]=new char[1]; 
chars[0]=primaryCode; 

// retrieve the key events that could have produced this glyph 
evs=myMap.getEvents(chars); 

if (evs != null){ 
    // we can reproduce this glyph with this key event array 
    for (int i=0; i< evs.length;i++) mySendKeyMethodHelper(evs[i]); 
} 
else { /* could not find a way to reproduce this glyph */ } 
+0

返信いただきありがとうございました。私はonKeyメソッドをオーバーライドしようとしましたが、まだ動作していません。 CustomKeyboardViewと共に上記のようにカスタムレイアウトをロードしています。メッセージングアプリケーションの「To」フィールドをクリックすると、私たちのレイアウトでキーボードが起動していることがわかりますが(上記のEditTextを参照)、キーボードを使用して入力したすべてのテキストカスタムIME)は、現在表示されているEditTextではなく、Messagingアプリケーションの 'To'フィールドに移動します。 IMEは 'To'フィールドにバインドされており、変更できません。 – nilMoBile

+1

*はフォーカスを持っているフィールド(あなたの場合はToフィールド)に*割り当てられています。デザインごとに。あなたはまだあなたのテキストビューにそれらを延期するonKeyでキーイベントを得ることができます。 –

+1

キーを取得する機会を得るためにOnKeyboardActionListenerを実装する必要があることを忘れてしまいました。更新されたコードを参照してください。 –

関連する問題