Create Custom Keyboard in Android

2019-01-13 22:22发布

问题:

I'd like to write an custom keyboard which should work on all devices that are running Android 4.0 and up. So first I searched the net but didn't found anything about that. So how can I create an app which replaces the stock keyboard of Android?

What I'd like to know:

  1. Is there a good tutorial out there? Do you guys have sample code?
  2. Do I need root to do this?
  3. What's the structure behind it? (Is it just a regular Activity with a Service??)
  4. Is it possible to read out the Inputbox within the keyboard?

回答1:

So, I did this for about 2 years, when I worked on Swype. You don't need root, you just need to implement an InputMethodService. You can get the text out of most textboxes, but not all (not all edit fields correctly implement their half of the APIs. Particularly anything with an input type INPUT_TYPE_NULL will not work well). To get the text you would call inputConnection.getExtractedText

Be warned- the API is bad, and apps are hit and miss on working with it. A basic keyboard is easy, but try and do anything complex and you'll spend a lot of time. A lot of man hours went in to the big keyboards



回答2:

So first I searched the net but didn't found anything about that

Creating an input method is covered in the Android developer documentation, which is on the Internet.

Do you guys have samplecode?

There is a sample in the samples/ directory of your SDK installation, if you downloaded sample code from the SDK Manager. Look for SoftKeyboard.

Do I need root to do this?

No.

What's the structure behind it? (Is it just a regular Activity with a Service??)

It is an InputMethodService.



回答3:

  1. Is there a good tutorial out there? Do you guys have sample code?

Hereby a good tutorial:
http://code.tutsplus.com/tutorials/create-a-custom-keyboard-on-android--cms-22615

  1. Do I need root to do this?

No, root is not required to do this.

  1. What's the structure behind it? (Is it just a regular Activity with a Service??)

You need to use the inputMethodService, see the documentation.

  1. Is it possible to read out the Inputbox within the keyboard?

root: no

Is it possible to read out the Inputbox within the keyboard?

yes it is possible.

InputConnection ic = getCurrentInputConnection();
String currentText = ic.getExtractedText(new ExtractedTextRequest(), 0).text;