問題描述
我有一個通用的 EditText.這很奇怪,因為我在使用硬鍵盤時無法對焦.上下文條件:
I have a common EditText. It's very strange because I can't focus it when use hard keyboard. Context condition:
- 打開 Droid 的硬鍵盤
- 開始活動
- 點擊editText輸入
- 輸入失敗.當您按任意鍵時,editText 會失去焦點.
要獲得焦點:按 Dpad,您將看到焦點從屏幕中的第一個小部件開始.最后關注目標EditText.然后就可以輸入了.沒有這個,你根本無法用硬鍵盤輸入.
To get focus: press Dpad and you will see the focus starts from the 1st widget in the screen. And finally focus on the target EditText. Then you can input. Without this, you can't input with hard keyboard at all.
軟鍵盤沒有這樣的焦點問題.
Soft keyboard doesn't have such focus problem.
我使用的是安卓 2.2.這是系統錯誤嗎?
I am using android 2.2. Is this a system bug?
推薦答案
如上所述,這顯然是硬鍵盤的錯誤.如果您的布局中有一個 EditText 和一個 TabHost,則在按下第一個鍵時,EditText 失去焦點并且按鍵被發送到活動.這是解決此問題的方法.在您的活動中實現這一點.
As mentioned above this is clearly a bug with hard keyboard. If you have an EditText and a TabHost in your layout, on first key pressed, EditText lose focus and key press is sent to the activity instead. Here is a work around to this problem. Implement this in your activity.
@Override
public boolean onKeyDown(int keyCode, KeyEvent event){
final EditText myInputField = (EditText) findViewById(R.id.MyInputEditText);
// this will happen on first key pressed on hard-keyboard only. Once myInputField
// gets the focus again, it will automatically receive further key presses.
if (!myInputField.hasFocus()){
myInputField.requestFocus();
myInputField.onKeyDown(keyCode, event);
}
return super.onKeyDown(keyCode, event);
}
如果您有多個 EditText 字段,則需要在類變量中跟蹤當前聚焦的 EditText 并在 onKeyDown 方法中使用它.
if you have multiple EditText fields, you will need to keep track of currently focused EditText in a class variable and use it in onKeyDown method.
這篇關于硬鍵盤無法聚焦editText的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!