硬键盘无法聚焦editText

Hard keyboard Fail to focus editText(硬键盘无法聚焦editText)

本文介绍了硬键盘无法聚焦editText的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通用的 EditText.这很奇怪,因为我在使用硬键盘时无法对焦.上下文条件:

I have a common EditText. It's very strange because I can't focus it when use hard keyboard. Context condition:

  1. 打开 Droid 的硬键盘
  2. 开始活动
  3. 点击editText输入
  4. 输入失败.当您按任意键时,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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:硬键盘无法聚焦editText

基础教程推荐