我正在为设备创建自己的 IME。我需要添加的是键盘 View 上方的文本框,如下图所示。虽然我可以如下图所示显示它,但我无法在其中写入文本。

我正在扩展键盘 View ,下面是布局结构

<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>

公共(public)类 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);
    }
}

谢谢,

最佳答案

您可以通过实现 KeyboardView.OnKeyboardActionListener 捕获您的软键盘事件并将它们传输到您自己的小部件。

在您的 Inputmethodservice.onKey() 方法中,您应该尝试将事件传输到您的 InputView subview ,如下所示:

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) {}
}

编辑

要回答您对字形和键码之间差异的评论,这里有一个可以帮助您的代码 fragment :
//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 */ }

关于android - InputKeyboard 上的 EditText 启动在 android 中创建 IME,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7865719/

10-09 07:04