Android:如何/在何处将手势代码放入 IME 中?

发布于 2024-08-31 10:36:07 字数 4555 浏览 10 评论 0原文

我是 Android 新手,但我正在尝试创建一个允许手势字符识别的 IME。我已经可以制作执行手势识别的简单应用程序,但不确定在哪里使用 IME 挂钩手势视图/obj。这是到目前为止我所拥有的 IME 的起始框架。我想使用android.gesture.Gesture/Prediction/GestureOverlayView/OnGesturePerformedListener。有人有建议吗?

gestureIME.java

public class gestureIME extends InputMethodService {
    private static Keyboard keyboard;
    private static KeyboardView kView;

    private int lastDisplayWidth;

@Override public void onCreate() {
    super.onCreate();
}

@Override public void onInitializeInterface() {      
 int displayWidth;

    if (keyboard != null) {
     displayWidth = getMaxWidth();

        if (displayWidth == lastDisplayWidth) return;
        else lastDisplayWidth = getMaxWidth();
    }

    keyboard = new GestureKeyboard(this, R.xml.keyboard);
}

@Override public View onCreateInputView() {
    kView = (KeyboardView) getLayoutInflater().inflate(R.layout.input, null);
    kView.setOnKeyboardActionListener(kListener);
    kView.setKeyboard(keyboard);

    return kView;
}

@Override public View onCreateCandidatesView() {
 return null;
}

@Override public void onStartInputView(EditorInfo attribute, boolean restarting) {
    super.onStartInputView(attribute, restarting);

    kView.setKeyboard(keyboard);
    kView.closing();                  //what does this do???
}   

@Override public void onStartInput(EditorInfo attribute, boolean restarting) {
    super.onStartInput(attribute, restarting);

}  

@Override public void onFinishInput() {
    super.onFinishInput();
}


public KeyboardView.OnKeyboardActionListener kListener = new KeyboardView.OnKeyboardActionListener() {
 @Override public void onKey(int keyCode, int[] otherKeyCodes) {
  if(keyCode==Keyboard.KEYCODE_CANCEL) handleClose();
  if(keyCode==10) getCurrentInputConnection().commitText(String.valueOf((char) keyCode), 1); //keyCode RETURN
 }

@Override public void onPress(int primaryCode) {}  // TODO Auto-generated method stub
@Override public void onRelease(int primaryCode) {}  // TODO Auto-generated method stub
@Override public void onText(CharSequence text) {}  // TODO Auto-generated method stub
@Override public void swipeDown() {}     // TODO Auto-generated method stub
@Override public void swipeLeft() {}     // TODO Auto-generated method stub 
@Override public void swipeRight() {}     // TODO Auto-generated method stub 
@Override public void swipeUp() {}      // TODO Auto-generated method stub
};

private void handleClose() {
    requestHideSelf(0);
    kView.closing();
}
}

GestureKeyboard.java

    package com.android.jt.gestureIME;

    import android.content.Context;
    import android.inputmethodservice.Keyboard;

    public class GestureKeyboard extends Keyboard {
        public GestureKeyboard(Context context, int xmlLayoutResId) {
            super(context, xmlLayoutResId);
        }
}

GesureKeyboardView.java

package com.android.jt.gestureIME;

import android.content.Context;
import android.inputmethodservice.KeyboardView;
import android.inputmethodservice.Keyboard.Key;
import android.util.AttributeSet;

public class GestureKeyboardView extends KeyboardView {
    public GestureKeyboardView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

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

    @Override protected boolean onLongPress(Key key) {
        return super.onLongPress(key);
    }

}

Keyboard.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 android:rowEdgeFlags="bottom">
         <Key android:codes="-3" android:keyLabel="Close"  android:keyWidth="20%p" android:keyEdgeFlags="left"/>
         <Key android:codes="10" android:keyLabel="Return" android:keyWidth="20%p" android:keyEdgeFlags="right"/>
    </Row>
</Keyboard>

input.xml

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

<com.android.jt.gestureIME.GestureKeyboardView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/gkeyboard"
        android:layout_alignParentBottom="true"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />

I'm new to Android but I'm trying to create an IME that allows for gesture-character recognition. I can already do simple apps that perform gesture recognition but am not sure where to hook in the gesture views/obj with an IME. Here is a starting skeleton of what I have for the IME so far. I would like to use android.gesture.Gesture/Prediction/GestureOverlayView/OnGesturePerformedListener. Does anyone have advice?

gestureIME.java

public class gestureIME extends InputMethodService {
    private static Keyboard keyboard;
    private static KeyboardView kView;

    private int lastDisplayWidth;

@Override public void onCreate() {
    super.onCreate();
}

@Override public void onInitializeInterface() {      
 int displayWidth;

    if (keyboard != null) {
     displayWidth = getMaxWidth();

        if (displayWidth == lastDisplayWidth) return;
        else lastDisplayWidth = getMaxWidth();
    }

    keyboard = new GestureKeyboard(this, R.xml.keyboard);
}

@Override public View onCreateInputView() {
    kView = (KeyboardView) getLayoutInflater().inflate(R.layout.input, null);
    kView.setOnKeyboardActionListener(kListener);
    kView.setKeyboard(keyboard);

    return kView;
}

@Override public View onCreateCandidatesView() {
 return null;
}

@Override public void onStartInputView(EditorInfo attribute, boolean restarting) {
    super.onStartInputView(attribute, restarting);

    kView.setKeyboard(keyboard);
    kView.closing();                  //what does this do???
}   

@Override public void onStartInput(EditorInfo attribute, boolean restarting) {
    super.onStartInput(attribute, restarting);

}  

@Override public void onFinishInput() {
    super.onFinishInput();
}


public KeyboardView.OnKeyboardActionListener kListener = new KeyboardView.OnKeyboardActionListener() {
 @Override public void onKey(int keyCode, int[] otherKeyCodes) {
  if(keyCode==Keyboard.KEYCODE_CANCEL) handleClose();
  if(keyCode==10) getCurrentInputConnection().commitText(String.valueOf((char) keyCode), 1); //keyCode RETURN
 }

@Override public void onPress(int primaryCode) {}  // TODO Auto-generated method stub
@Override public void onRelease(int primaryCode) {}  // TODO Auto-generated method stub
@Override public void onText(CharSequence text) {}  // TODO Auto-generated method stub
@Override public void swipeDown() {}     // TODO Auto-generated method stub
@Override public void swipeLeft() {}     // TODO Auto-generated method stub 
@Override public void swipeRight() {}     // TODO Auto-generated method stub 
@Override public void swipeUp() {}      // TODO Auto-generated method stub
};

private void handleClose() {
    requestHideSelf(0);
    kView.closing();
}
}

GestureKeyboard.java

    package com.android.jt.gestureIME;

    import android.content.Context;
    import android.inputmethodservice.Keyboard;

    public class GestureKeyboard extends Keyboard {
        public GestureKeyboard(Context context, int xmlLayoutResId) {
            super(context, xmlLayoutResId);
        }
}

GesureKeyboardView.java

package com.android.jt.gestureIME;

import android.content.Context;
import android.inputmethodservice.KeyboardView;
import android.inputmethodservice.Keyboard.Key;
import android.util.AttributeSet;

public class GestureKeyboardView extends KeyboardView {
    public GestureKeyboardView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

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

    @Override protected boolean onLongPress(Key key) {
        return super.onLongPress(key);
    }

}

keyboard.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 android:rowEdgeFlags="bottom">
         <Key android:codes="-3" android:keyLabel="Close"  android:keyWidth="20%p" android:keyEdgeFlags="left"/>
         <Key android:codes="10" android:keyLabel="Return" android:keyWidth="20%p" android:keyEdgeFlags="right"/>
    </Row>
</Keyboard>

input.xml

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

<com.android.jt.gestureIME.GestureKeyboardView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/gkeyboard"
        android:layout_alignParentBottom="true"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文