问题
I am new to android. I am trying to create an application with a custom keyboard, used for the application only. Not a general keyboard for distribution. Basically my test is to have a layout like:
Relative Layout
Relative Layout id tolayouot
EditText id userID
Button
LinearLayout alignbottomparent=TRUE
InputMethodService.KeyboardView id kv
So there is a top section with an Edit Box and a button and the bottom section is the keyboard. When I press the button, the Custom Keyboard appears. Then I would like to be able to type into the EditText box.
I have made a private variable to implement keyboardactionlistener. It basically follows and example and only puts Log messages out.
private OnKeyboardActionListener mykal = new OnKeyboardActionListener()
{
@Override
public void swipeUp() {
Log.d(TAG, "swipeUp");
}
@Override
public void swipeRight() {
Log.d(TAG, "swipeRight");
}
@Override
public void swipeLeft() {
Log.d(TAG, "swipeLeft");
}
@Override
public void swipeDown() {
Log.d(TAG, "swipeDown");
}
@Override
public void onText(CharSequence text) {
Log.d(TAG, "onText? \"" + text + "\"");
}
@Override
public void onRelease(int primaryCode) {
Log.d(TAG, "onRelease? primaryCode=" + primaryCode);
}
@Override
public void onPress(int primaryCode) {
Log.d(TAG, "onPress? primaryCode=" + primaryCode);
}
@Override
public void onKey(int primaryCode, int[] keyCodes) {
Log.d(TAG, "onKey? primaryCode=" + primaryCode);
int n1 = 0; // -1 count
for (int keyCode : keyCodes) {
if (keyCode == -1) {
n1++;
continue;
}
Log.v(TAG, "keyCode=" + keyCode);
}
Log.v(TAG, "keyCode=-1 *" + n1);
}
};
}
Here is the on create code
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_example);
Button b = (Button) this.findViewById(R.id.tstbtn);
myedit = (EditText) this.findViewById(R.id.user_ID);
rl = (RelativeLayout) this.findViewById(R.id.toplayout);
b.setOnClickListener(clistner);
KeyboardView keyboardView = (KeyboardView) findViewById(R.id.keyboardView);
Keyboard keyboard = new Keyboard(this, R.xml.tstkbd);
keyboardView.setKeyboard(keyboard);
keyboardView.setEnabled(true);
keyboardView.setPreviewEnabled(true);
myedit.setOnTouchListener(mytouch);
// keyboardView.setOnKeyListener(this);
keyboardView.setOnKeyboardActionListener(mykal);
//hide the default keyboard
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
So when I run my test, the default keyboard disappears just like I want, I click the button and my keyboard appears. All of the log messages in the keyboardactionlistener show up in the debugger when I press the keys, so the keyboard is working. I can set the focus to the EditText but if I click a key on my custom keyboard, I do not get any text in the EditText box.
Is there a way to smoothly get the keypresses from the custom keyboard to register in the EditText box?
I know I can use the OnKey Event of the KeyBoardactionListener to manipulate the text in the EditText box but I would like to use an intrinsic function.
I have tried:
- Sending an Event Message to the EditText and to the RelativeLayout toprl but that crashed.
- Setting an onTouchListener for the EditText.
Is the only way to accomplish this to create an InputMethodService?
Can anyone please explain why this doesnt work???
The two examples below were the ones I was trying to use.
http://androidpadanam.wordpress.com/2013/05/29/customkeyboard-example/ http://www.infiniterecursion.us/2011/02/android-activity-custom-keyboard.html
回答1:
The reason nothing is showing in the EditText is because you're not doing anything with the events when you receive them. You have to respond to the listener events and implement the key press yourself. Below is an example of how you could implement an action to add text.
@Override
public void onRelease(int primaryCode) {
Log.d(TAG, "onRelease? primaryCode=" + primaryCode);
myedit.setText(myedit.getText().toString() + getKeyForPrimaryCode(primaryCode));
}
Note: getKeyForPrimaryCode would just be a method you make which converts the primary code to the label on the key.
来源:https://stackoverflow.com/questions/19262563/use-an-edittext-with-custom-keyboard