LWUIT 开发教程(20):自定义软键盘按键

有时需要自定义软键盘上按键,比如一个搜索框,希望把缺省的“OK”键,改成“Search”,当用户点击“Search”时完成搜索操作。

下面代码修改软键盘的按键定义,并可以为“Search”按钮添加事件处理。

Form mainForm = new Form("TextField  Demo");

mainForm.setLayout(new BorderLayout());

TextField textfield = new TextField();
String[][] SEARCH_QWERTY = new String[][]{
 {"q", "w", "e", "r", "t", "y", "u", "i", "o", "p"},
 {"a", "s", "d", "f", "g", "h", "j", "k", "l"},
 {"$Shift$", "z", "x", "c", "v", "b", "n", "m", "$Delete$"},
 {"$Mode$", "$Space$", "$Search$"}
};
VirtualKeyboard vkb = new VirtualKeyboard();
 //add the new input mode
vkb.addInputMode("ABC_S", SEARCH_QWERTY);
vkb.setInputModeOrder(new String[]{"ABC_S"});
//add the new special button to the vkb
vkb.addSpecialButton("Search", new Command("Search") {

 public void actionPerformed(ActionEvent evt) {
 //search logic...
 }
});

VirtualKeyboard.bindVirtualKeyboard(textfield, vkb);


mainForm.addComponent(BorderLayout.CENTER, textfield);