Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
This repository was archived by the owner on Feb 26, 2023. It is now read-only.

@FocusChange and @EditorAction #1767

@branoh

Description

@branoh

Problem description

If @FocusChange annotation is used altogether with @EditorAction annotation on the same text view(or any TextView subclass), project fails to compile because of the code generated by Android Annotations.

Version of the AA: 4.0.0

Example & Explanation

Annotate methods like this:

    @FocusChange(R.id.edit_text)
    protected void onFocusChange() {
        ...
    }

    @EditorAction(R.id.edit_text)
    protected void onEditorAction() {
        ...
    }

...and the compilation fails because of the generated code which looks like this:

    @Override
    public void onViewChanged(HasViews hasViews) {
        View view_edit_text = hasViews.findViewById(R.id.edit_text);

        if (view_edit_text!= null) {
            view_edit_text.setOnFocusChangeListener(new OnFocusChangeListener() {

                @Override
                public void onFocusChange(View view, boolean hasFocus) {
                    MainActivity_.this.onFocusChange();
                }
            }
            );
            view_edit_text.setOnEditorActionListener(new OnEditorActionListener() {

                @Override
                public boolean onEditorAction(TextView textView, int actionId, KeyEvent event) {
                    MainActivity_.this.onEditorAction();
                    return true;
                }
            }
            );
        }
    }

As you can see view_edit_text variable is typed as an instance of View class, which of course does not have the setOnEditorActionListener method and therefore the generated code could not be compiled.

Workaround

I found a workaround how to get a compilable piece of code: inject the EditText using @ViewById(R.id.edit_text) annotation and the generated code is now compilable because in the generated code, there's the injected field used to call methods on, which has a correct type.

Inject EditText(or any TextView or it's subclass):

    @ViewById(R.id.edit_text)
    protected EditText mEditText;

Now the generated code looks like this:

    @Override
    public void onViewChanged(HasViews hasViews) {
        this.mEditText = ((EditText) hasViews.findViewById(R.id.edit_text));
        if (this.mEditText!= null) {
            this.mEditText.setOnFocusChangeListener(new OnFocusChangeListener() {

                @Override
                public void onFocusChange(View view, boolean hasFocus) {
                    MainActivity_.this.onFocusChange();
                }
            }
            );
            this.mEditText.setOnEditorActionListener(new OnEditorActionListener() {

                @Override
                public boolean onEditorAction(TextView textView, int actionId, KeyEvent event) {
                    MainActivity_.this.onEditorAction();
                    return true;
                }
            }
            );
        }
    }

Expected solution

Generate compilable code:

  • Either make the view_edit_text have a correct type
TextView view_edit_text = (TextView) hasViews.findViewById(R.id.edit_text);
  • Or cast it to correct type when calling the setOnEditorActionListener method
((TextView)view_edit_text).setOnEditorActionListener(...);

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions