Android:如何/在何处将手势代码放入 IME 中?
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论