Let's go on with our keyboard.
This post is telling you how to control our new keyboard in activities.
First, you need to implement two listeners called OnKeyboardActionListener and OnKeyListener like:
Next, initial your keyboard view, to apply the design you did in step one. Do this in OnCreate method:
Then add a click event to your editors that make the keyboard appearing:
Here I use a variable called currentEditText to save the touched EditText, in order to tell the keyboard which EditText should append the character:
EditText currentEditText = null; //add it in activity class before OnCreate method
Now, your apps can not be compiled because you should implement methods in Interfaces:
OK, now you can run your apps, when you touch a EditText(if you already have one), the keyboard may appears, touch again, it disappears.
But when you try to touch any key in your new keyboard, nothing happens. Now, we need to work on this.
Remember the codes I talked about on my last post? now we should have something to record the relation ship between the codes and characters.
I'm using a HashMap, surly you can use any other things you want, like, maybe, another XML file, or try to read directory from the keyboard XML file.
Add this in onKey method(or any place you want, but make sure keyCodeMap is visitable in onKey):
Then we should append charaters into the EditView when user press the key. In onKey:
what's "else" working here? codes "-5" is the delete(back) key, so we are going to delete one charater each time user press it.
OK, now the keyboard is finished, but you may find that the default Input Method is still there. We must disable it.
We have many ways to do so, here I'm using the config of the EditText:
If you have any good ideas about disabling the default Input Method, please comment and tell me.
OK, all done, test your custom keyboard in your own apps, have Fun!
Comment this post if you have any question, or you find any problem on my post.