棒棒糖中未调用 setOnEditorActionListener

setOnEditorActionListener is not called in lollipop

我想创建一个布局,用户可以在其中键入艺术家姓名,当他在虚拟键盘上按下搜索时,会显示艺术家列表。

View rootView = (View) inflater.inflate(R.layout.fragment_search, container, false);
EditText searchArtist = (EditText) rootView.findViewById(R.id.searchArtist);
searchArtist.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
            makeToast();
            return true;
        }
        return false;
    }
});

这是我的xml

<EditText
    android:id="@+id/searchArtist"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="@string/search_artist"
    android:imeOptions="actionSearch"
    android:inputType="text">
    <requestFocus />

我在其他 Whosebug post 中搜索了很多关于此的内容,但 none 的解决方案似乎有效。

MinSDK 21,在 nexus 6 上测试,compileSdkVersion 22,buildToolsVersion '22.0.1'。

另请注意,searchArtist(edittext) 在片段内。

更多上下文:侦听器代码在 onCreateView 方法中。

按照建议here,尝试在编辑器操作上监听按键事件。

可以找到文档here

我在 Android 5.1.1 上测试了类似的代码。有用。 我建议插入一条日志消息以检查是否正在调用 onEditorAction:

public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
     Log.v("TAG", "onEditorAction(): " + actionId);  
     if (actionId == EditorInfo.IME_ACTION_SEARCH) {
         Log.v("TAG", "onEditorAction(): entered");
     ...

这样,您可以确保该方法确实被调用。 EditorInfo.IME_ACTION_SEARCH 是常量:3

我假设 makeToast();是您创建的方法,对吗?

为了添加更多信息,我在片段中测试了您的代码。所以,我真的不相信问题出在你的代码被插入的地方。您只需要确保在需要使用它之前设置了监听器。 希望对您有所帮助

public class MainFragment extends Fragment {
....
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

        // view is a reference to whole fragment layout (where searchArtist is inserted)
        View view = inflater.inflate(R.layout.fragment, null);

        // Find searchArtist (There's only one @id/searchArtist inside View)
        EditText searchArtist = (EditText)view.findViewById(R.id.searchArtist);

        searchArtist.setOnEditorActionListener(new  TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                Log.v("TEMP", "I'm being clicked " + actionId);
                return true;
            }
            return false;
        }
    });
    return view;
    }// End of onCreateView
} // End of Fragment

我的xml:

<EditText android:id="@+id/searchArtist"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"    
     android:hint="@string/search_artist"    
     android:imeOptions="actionSearch"  
     android:inputType="text" />

在尝试了所有解决方案之后,包括 setOnKeyListener 和 textChangeListener。最终的解决方案是删除项目并从头开始。估计是环境问题。

我真的很喜欢 Android Studio,但有时这些错误会让我退缩。

我遇到了同样的问题: OnEditorActionListener 和 onKeyListener 都不起作用。

但 TextChangeListener 解决方案成功了。对我来说。

片段:

protected void onCreate(Bundle savedInstanceState){
  ...
  someField = (EditText) this.findViewById(R.id.some_field);
  someField.addTextChangeListener(someListener);
  ...
}