問(wèn)題描述
我想覆蓋虛擬鍵盤的 ENTER 鍵的行為,以便:
I want to override the behaviour of the ENTER key of the virtual keyboard so that:
- 當(dāng)屏幕上有更多字段時(shí),它會(huì)制表"到下一個(gè)字段
- 當(dāng)它是屏幕的最后一個(gè)字段時(shí),它執(zhí)行屏幕的默認(rèn)動(dòng)作
我一直在使用 IME 選項(xiàng)和標(biāo)簽,但沒(méi)有得到我想要的.有人有什么建議嗎?
I've been playing with the IME options and labels, but just don't get what I want. Anybody have any suggestions?
推薦答案
在另一個(gè)論壇的幫助下,我找到了方法.
With help on another forum, I found the way to do it.
為了使其可重用,我創(chuàng)建了自己的超級(jí)對(duì)話框類,其中包含 2 個(gè) OnKeyListener
對(duì)象和一個(gè)抽象提交方法:
To make it reusable, I have created my own super dialog class that contains 2 OnKeyListener
objects and an abstract submit method:
public abstract class MyAbstractDialog extends Dialog {
/**
* OnKeyListener that puts the focus down when the ENTER key is pressed
*/
protected View.OnKeyListener onEnterFocusDown = new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
v.requestFocus(View.FOCUS_DOWN);
return true;
}
return false;
}
};
/**
* OnKeyListener that submits the page when the ENTER key is pressed
*/
protected View.OnKeyListener onEnterSubmitView = new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
submitView(v);
return true;
}
return false;
}
};
protected abstract void submitView(View v);
}
現(xiàn)在在 Dialog
我可以使用這些對(duì)象來(lái)設(shè)置我的字段:
Now in the Dialog
I can use these objects to set on my fields:
// make the ENTER key on passwordField1 put the focus on the next field
passwordField1.setOnKeyListener(onEnterFocusDown);
// make the ENTER key on passwordField2 submit the page
passwordField2.setOnKeyListener(onEnterSubmitView);
這篇關(guān)于如何覆蓋 <ENTER>Android中虛擬鍵盤的按鍵行為的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!