首页 > 解决方案 > OnEditorActionListener 中的 requestFocus()

问题描述

下面是我为将焦点从一个编辑文本转移到另一个而编写的一段代码,但是我在这个实现中遇到的问题是,焦点移动到特定的编辑文本,然后突然移动到下一个编辑文本. 所以,我无法输入任何内容,因为焦点根本不在于特定的编辑文本..

//some code comes here
// gun_pistolNotes and gun_pistolModel are two diff. editText
......
......
    gun_pistolNotes.setOnEditorActionListener(new OnEditorActionListener(){
    @Override
    public boolean onEditorAction(TextView view,int actionId, KeyEvent event){
    if(actionId == EditorInfo.IME_ACTION_UNSPECIFIED){
        gun_pistolModel.requestFocus(); // moves to this edittext and suddenly moves to another editext 
        return true;
    }
    return false;
    }
  });

......
......
//some code comes here

任何帮助表示赞赏。

标签: androidandroid-edittextandroid-view

解决方案


如果你不需要实现任何相关的逻辑,你也可以直接在你的布局文件中实现,而不是通过程序来实现,使用android:nextFocusDown如下所示的属性:

...
...
<EditText
        android:id="@+id/gun_pistolNotes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:nextFocusDown="@+id/gun_pistolModel" />

    <EditText
        android:id="@+id/gun_pistolModel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

...
...

推荐阅读