尝试创建自定义View.onFocusChangeListener时出错

时间:2014-12-12 17:24:04

标签: android listeners

我尝试自定义View.onFocusChangeListener类,如下所示:

private class myCostBoxFocusListener implements View.onFocusChangeListener {

    public void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {

        //custom stuff here

    }
}

我使用:

设置监听器
costBox.setOnFocusChangeListener(new myCostBoxFocusListener());
//costBox is an EditText declared and instantiated earlier.

但是,为了创建类,我得到编译器错误

"View.onFocusChangeListener cannot be resolved to a type"

并设置监听器

"The method setOnFocusChangeListener(View.OnFocusChangeListener) in the type 
View is not applicable for the arguments (OCRMain.myCostBoxFocusListener)"
//OCRMain is the name of the class I'm working in.

我在导入中导入了android.view.View.OnFocusChangeListener,我真的不确定为什么会这样,因为我还为EditText.OnEditorActionListener创建了一个没有错误的自定义类。我尝试在View.onFocusChangeListener和EditText.onFocusChangeListener之间更改我的代码而不做任何更改。我已经尝试过清洁和重建项目而没有任何变化。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

答案很简单时难道不是很好吗?感谢Mike M.这个;我一直看到“OnFocusChanged”的引用,坦率地说,我并不完全确定它的区别以及何时/为什么一个适合另一个。如上所述,我还将第一个“o”大小写错误。但将其更改为以下内容:

private class myCostBoxFocusListener implements View.OnFocusChangeListener {

    public void onFocusChange(View v, boolean hasFocus) {

        //stuff

    }
}