需要识别视图类型

Need to identify type of View

Android。 API16(我知道,老了但我改不了)

我需要以编程方式确定焦点视图的类型。 我有一个能够生成数据的线程,但并不总是正确的视图处于焦点 (EditText)。 我需要检查,当数据准备好时,焦点中的当前视图是否可以处理数据。 如果是,好的,如果不是,则数据只是 discarded/lost。 我知道其中一个可能的焦点视图可以是 ListView。

最好的方法是什么?

我尝试使用 activity.getCurrentFocus().getContentDescription() 但它崩溃了。

有什么建议吗?

谢谢!

为此,您可以使用 instanceof

if (activity.getCurrentFocus() instanceof ListView){
    // do whatever
} else {
    // do other
}

另一个选项是检查引用是否相等。如果您参考了您希望关注的视图,只需检查每个视图:

View inFocus = activity.getCurrentFocus()
if (inFocus == myView){
    // do whatever
} else if (inFocus == myOtherView){

} else {...}