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

标签 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 之间更改我的代码,但没有任何更改。我已尝试清理并重建项目,但不做任何更改。

非常感谢任何帮助。

最佳答案

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

private class myCostBoxFocusListener implements View.OnFocusChangeListener {

    public void onFocusChange(View v, boolean hasFocus) {

        //stuff

    }
}

关于android - 尝试创建自定义 View.onFocusChangeListener 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27448874/

相关文章:

java - toast 线从未被调用

actionscript-3 - 如何在 AS3 中一次删除所有事件监听器

Android Spinner - 如何将列表选择默认为无

java - 无法解析对象

android - Realm Android : io. realm.exceptions.RealmException: 'DomainModel' 在当前架构中不存在

extjs - 切换对话框中一组字段的可见性

java - 如何获取触发的 Geo-Fire 查询的 key

android - 带有 fragment 的重复 ID

android - 如何更改 React Native 和 Metro Bundler 端口

android - 如何在android map 中勾勒出文字轮廓