【问题标题】:requestFocus() in OnEditorActionListenerOnEditorActionListener 中的 requestFocus()
【发布时间】:2019-06-20 21:51:42
【问题描述】:

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

//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

感谢任何帮助。

【问题讨论】:

    标签: android android-edittext android-view


    【解决方案1】:

    如果你不需要实现任何相关的逻辑,也可以直接在你的布局文件中实现,使用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" />
    
    ...
    ...
    

    【讨论】:

    • 我需要在 OnEditorActionListener 中实现逻辑(因为我与 iOS 共享代码)。所以当用户按下返回键并在 OnEditorActionListener 中自己执行此操作时,我必须从编辑中断开自动聚焦到下一个编辑的能力
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-22
    • 1970-01-01
    • 2012-09-26
    • 1970-01-01
    • 2014-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多