不需要投射 findViewById 的结果吗? [英] No need to cast the result of findViewById?

查看:35
本文介绍了不需要投射 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,所以我想知道为什么我们不需要cast这个类?

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天全站免登陆