android - InputKeyboard 上的 EditText 启动 在 android 中创建 IME

标签 android android-input-method

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

enter image description here

我正在扩展键盘 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/

相关文章:

android - 更改 WebView 的 android 输入法

android - 在当前 fragment 中更改 ActionBar 标题

android - Chrome for Android 中的 CSS 问题

java - Android 上隐藏键盘会导致应用程序崩溃

Android键盘布局语言

java - 带有清除按钮的 Android java android.support.design.widget.TextInputLayout

android - 开始在 InputMethodService 中提取文本时出现意外的 null

android - 自定义单选按钮以提供类似 tabhost 的功能

android - 如何在 MediaPlayer 上设置代理

android - 自定义android应用权限描述内容