简体   繁体   中英

still getting error: 'implement abstract method 'onFocusChange(View, boolean)' in 'OnFocusChangeListener' even when the method has been implemented

I'm trying to implement OnFocusChangeListener on an EditText . The problem is I'm getting this error

Class 'FacilityScreen' must either be declared abstract or implement abstract method 'onFocusChange(View, boolean)' in 'OnFocusChangeListener'

even though the method onFocusChange has been implemented in OnFocusChangeListener .

Here is my code

 public class FacilityScreen extends AppCompatActivity implements AdapterView.OnItemSelectedListener,View.OnFocusChangeListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_facilty_screen);

        EditText editText = (EditText) findViewById(R.id.enter_location);
        editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {

            @Override
            public void onFocusChange(View view, boolean hasfocus) {
                if(hasfocus){
                     //do something
                }
            }
        });
    }
}

What am I getting wrong?

You need to @Override public void onFocusChange() method inside your activity

Than use like this editText.setOnFocusChangeListener(this)

Check this example

public class FacilityScreen extends AppCompatActivity implements  View.OnFocusChangeListener{

    EditText editText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_facilty_screen);
        editText = (EditText) findViewById(R.id.enter_location);
        editText.setOnFocusChangeListener(this);

    }


    @Override
    public void onFocusChange(View view, boolean b) {
        if(view==editText){
            // perfom your action here
        }
    }
}

Version 1 - handle listener on class level

If you want the class to handle the listener then you have to tell the view so:

editText.setOnFocusChangeListener(this);

Then you have to implement onFocusChange on the class level. Android Studio should help you to do this with a few clicks. The outcome should be something like:

 public class FacilityScreen extends AppCompatActivity implements AdapterView.OnItemSelectedListener,View.OnFocusChangeListener {
        //your other methods

        @Override
        public void onFocusChange(View view, boolean hasfocus) {
            // your code
        }
    }

Version 2 - handle listener inline as anonymous class

You are actually so far using an anonymous class to handle the listener inline. If you want to listen there and not on class level, then simply remove the OnFocusChangeListener from the class level. Ie remove implements AdapterView.OnItemSelectedListener,View.OnFocusChangeListener and leave the rest as it is.

You do not need to implement it in Activity, replace

 public class FacilityScreen extends AppCompatActivity implements AdapterView.OnItemSelectedListener,View.OnFocusChangeListener

to

 public class FacilityScreen extends AppCompatActivity

Alternatively, you could override the onFocusChange method in Activity itself and then set it in editText, like this:

public class FacilityScreen extends AppCompatActivity implements AdapterView.OnItemSelectedListener,View.OnFocusChangeListener
{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_facilty_screen);

        EditText editText = (EditText) findViewById(R.id.enter_location);
        editText.setOnFocusChangeListener(this);

    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        // do something
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    });
}

Your class implements an interface. That means that class (in your example: Activity), must Override all non-abstract methods in class. Here, you've mixed two approaches. If you implement an interface in your class declaration, you don't need to create new instance of inner class like

 new OnFocusChangeListener() {
//override a method
}

It's OK, to leave your code as it is, just remove. In this case, you use inner class approach. Creating anonymous instance and overriding a non-abstract method.

implements View.OnFocusChangeListener

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM