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

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

我正在尝试在 EditText 上实施 OnFocusChangeListener。问题是我收到此错误

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

即使 onFocusChange 方法已在 OnFocusChangeListener 中实施。

这是我的代码

 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
                }
            }
        });
    }
}

我哪里错了?

您需要 @Override public void onFocusChange() 方法在您的 activity

比这样使用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
        }
    }
}

版本 1 - 在 class 级别处理侦听器

如果你想让 class 处理监听器,那么你必须这样告诉视图:

editText.setOnFocusChangeListener(this);

然后你必须在 class 级别上实施 onFocusChangeAndroid Studio 只需点击几下即可帮助您完成此操作。结果应该是这样的:

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

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

版本 2 - 将内联侦听器处理为匿名 class

到目前为止,您实际上是在使用匿名 class 来处理内联侦听器。如果您想在那里而不是 class 级别收听,则只需从 class 级别删除 OnFocusChangeListener。 IE。删除 implements AdapterView.OnItemSelectedListener,View.OnFocusChangeListener 并保留其余部分。

不需要在Activity中实现,替换

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

 public class FacilityScreen extends AppCompatActivity

或者,您可以覆盖 Activity 本身的 onFocusChange 方法,然后在 editText 中设置它,如下所示:

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) {

    });
}

您的 class 实现了一个接口。这意味着 class(在您的示例中:Activity)必须覆盖 class 中的所有 non-abstract 方法。 在这里,您混合了两种方法。如果您在 class 声明中实现接口,则无需像

那样创建内部 class 的新实例
 new OnFocusChangeListener() {
//override a method
}

没关系,让您的代码保持原样,只需删除即可。在这种情况下,您使用内部 class 方法。创建匿名实例并覆盖 non-abstract 方法。

implements View.OnFocusChangeListener