本文介紹了如何覆蓋 <ENTER>Android中虛擬鍵盤的按鍵行為的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我想覆蓋虛擬鍵盤的 ENTER 鍵的行為,以便:
I want to override the behaviour of the ENTER key of the virtual keyboard so that:
- 當屏幕上有更多字段時,它會制表"到下一個字段
- 當它是屏幕的最后一個字段時,它執(zhí)行屏幕的默認動作
我一直在使用 IME 選項和標簽,但沒有得到我想要的.有人有什么建議嗎?
I've been playing with the IME options and labels, but just don't get what I want. Anybody have any suggestions?
推薦答案
在另一個論壇的幫助下,我找到了方法.
With help on another forum, I found the way to do it.
為了使其可重用,我創(chuàng)建了自己的超級對話框類,其中包含 2 個 OnKeyListener
對象和一個抽象提交方法:
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
我可以使用這些對象來設(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中虛擬鍵盤的按鍵行為的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!