无需强制转换findViewById的结果? [英] No need to cast the result of findViewById?

查看:162
本文介绍了无需强制转换findViewById的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我发现AndroidStudio提醒我删除一些类强制转换.我记得在过去,我们必须强制转换findViewById的结果,但是现在没有必要了.

Recently i found that AndroidStudio reminds me to remove some class cast. I remember that in the old time, we have to cast the result of findViewById, but now it's not necessary.

findViewById的结果仍然是View,所以我想知道为什么我们不需要强制转换类?

The result of findViewById is still View, so i want to know why we don't need to cast the class?

我找不到提到的任何文件,有人可以找到任何文件吗?

I can't find any documents mentioned that, can anyone find any document?

推荐答案

从API 26开始,findViewById使用推论作为其返回类型,因此您不再需要强制转换.

Starting with API 26, findViewById uses inference for its return type, so you no longer have to cast.

旧定义:

View findViewById(int id)

新定义:

<T extends View> T findViewById(int id)

因此,如果您的compileSdk至少为26,则意味着您可以使用此:)

So if your compileSdk is at least 26, it means that you can make use of this :)

这篇关于无需强制转换findViewById的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆