在EditText上在不同的设备只能处理回车键回车键、不同、设备、EditText

2023-09-05 06:17:23 作者:三年梦、七年痒

现在我使用的是一个onEditorActionListener正在处理回车键在我的EditText领域,着眼于行动ID为IME_NULL。它工作正常,我所有的用户只有一个除外。她有一个的Xperia弧。

Right now I'm handling the enter key in my EditText fields using a an onEditorActionListener and looking at the Action ID for IME_NULL. It works fine for all my users except one. She has an Xperia Arc.

TextView.OnEditorActionListener keyListener = new TextView.OnEditorActionListener(){
  public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
    if(actionId == EditorInfo.IME_NULL){
      if(((EditText)findViewById(view.getId())) == ((EditText)findViewById(R.id.etUser))){
        ((EditText) findViewById(R.id.etPass)).requestFocus();
      }
      if(((EditText)findViewById(view.getId())) == ((EditText)findViewById(R.id.etPass))){
        logon();
      }
    }
  return true;
  }
};

在了解这个问题,我试过另一种方法通过使用onKeyListener,寻找关键事件ACTION_DOWN然后检查键code,如果它匹配KEY code_ENTER。

After learning about the issue, I tried another approach by using an onKeyListener and looking for the key event ACTION_DOWN then checking the keycode if it matched KEYCODE_ENTER.

EditText etUserName = (EditText) findViewById(R.id.etUser);
etUserName.setOnKeyListener(new OnKeyListener() {
  public boolean onKey(View view, int keyCode, KeyEvent event){
    if (event.getAction() == KeyEvent.ACTION_DOWN){
      switch (keyCode)
      {
        case KeyEvent.KEYCODE_DPAD_CENTER:
        case KeyEvent.KEYCODE_ENTER:
          if(((EditText)findViewById(view.getId())) == ((EditText)findViewById(R.id.etUser))){
            ((EditText) findViewById(R.id.etPass)).requestFocus();
          }
          return true;
        default:
          break;
      }
    }
    return false;
  }
});

在没有骰子无论是。我茫然现在。中有很多应用服务,在那里,处理回车键就好了。他们在做什么不同?

No dice on that either. I'm at a loss right now. There are plenty of apps out there that handle the enter key just fine. what are they doing differently?

推荐答案

我想出如何得到它的工作。

I figured out how to get it to work.

我不得不添加机器人:单线=真在布局XML的标记的EditText(交替可以使用setSingleLine()在code设置)。这迫使编辑文本只使用一个线,重点将转到下一个的EditText框。

I had to add android:singleLine="true" to the EditText tag in the layout XML (alternately you can set it by using setSingleLine() in code). This forces the edit text to use only one line and focus will go to the next EditText box.