Android: Non-Keyboard IME

瘦欲@ 提交于 2019-12-04 11:42:36

问题


Im trying to create an IME for android that is not a traditional keyboard (rows of keys corresponding to the different letters) and I am having trouble finding helpful resources on how to do so since all of the code examples in the SDK use a Keyboard API and it's built in functions.

I've designed the interface of the IME in an XML file as though it were simply an Activity within in application but I'm getting lost in their demos since their Keyboards are simply objects that are built from a different style of XML structure.

If anyone has a link to an open source project that does not use the tradional keyboard structure or could simply guide me to displaying the UI that I've already constructed onto the IME window, I think I can figure the rest out.

If you need any more specfic information, please feel free to ask. Thanks in advance.


回答1:


The SoftKeyboard sample is in fact structured similarly to what you need, though you don’t know it. Let’s break it down — here’s a complete stub to act as an input method service and use the standard XML keyboard:

import android.inputmethodservice.InputMethodService;
import android.inputmethodservice.Keyboard;

public class MyInputMethod extends InputMethodService {
    private Keyboard mKeyboard;

    @Override public void onInitializeInterface() {
        mKeyboard = new Keyboard(this, R.xml.qwerty);
    }

    @Override public View onCreateInputView() {
        mInputView = (KeyboardView) getLayoutInflater().inflate(
                R.layout.input, null);
        mInputView.setKeyboard(mKeyboard);
        return mInputView;
    }
}

Note that two XML documents are named. The first, res/xml/qwerty.xml, defines the layout so that the KeyboardView class knows how to draw the keyboard.

But the layout which it inflates, res/layout/input.xml, consists of this (simplified):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android">
    <android.inputmethodservice.KeyboardView android:id="@+id/keyboard" />
</LinearLayout>

This is all you need to declaratively create a view! Creating a stand-alone View isn’t much different from creating an Activity. You don’t have the standard activity lifecycle, but both environments give you access to XML layouts. All you need to do is use the inflater, reference any subviews you need, and then return the main view.

So hopefully you’ll be able to inflate your layout after thinking about this.


You don’t even need to use XML layouts if your layout is simple enough. If your input method can consist entirely of a single view, you can just instantiate it directly in onCreateInputView. Here’s a complete stub input method service that doesn’t use any XML:

public class MyInputMethod extends InputMethodService {
    MyView view;

    @Override
    public View onCreateInputView() {
        return view = new MyView(this);
    }

}

(Of course the boilerplate in the manifest and res/xml/method.xml files would still be there.)



来源:https://stackoverflow.com/questions/6448627/android-non-keyboard-ime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!