Android O转换为findViewById不再需要了吗? [英] Android O casting to findViewById not needed anymore?

查看:128
本文介绍了Android O转换为findViewById不再需要了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在Android Studio中更新了我的android SDK和构建工具到API 26,我直接注意到当我做这样的事情时,Android Studio将我的视图转换为多余:

I recently updated my android SDK and build tools to API 26 in Android Studio and I directly noticed that Android Studio was marking my view casts as "redundant" when I do something like this:

TextView itemName = (TextView) findViewById(R.id.menuItemName);

经过一番研究,我发现自SDK 26起, findViewById 使用Java 8功能返回相同的对象类型,但我想知道的是删除所有强制转换是否完全安全。这会在26之前导致Android上出现任何问题吗?有关这方面的任何更多信息将有所帮助,因为我在互联网上找不到太多。在此先感谢。

After some research, I found that since SDK 26, findViewById uses Java 8 features to return the same object type, but what I wanted to know is if it is completely safe to remove all casts. Will this cause any issues on Android prior to 26? Any more info on this would be helpful as I didn't find much on the internet. Thanks in advance.

推荐答案

方法签名随着您的注意事项发生了变化,现在看起来像:

The method signature changed as you noticed and now it looks like:

public <T extends View> T findViewById(int id);

与旧版(SDK 26之前)相比:

compared to the old (pre SDK 26) one:

public View findViewById(int id);

只要您使用SDK 26(或更新版本)编译您的项目可以安全地从代码中删除转换,因为您将使用不再需要它的新 findViewById()

so as long as you use SDK 26 (or newer) to compile your project you can safely remove the casting from your code as you will be using new findViewById() which no longer requires it.


所以minSdk低于26会不会导致问题?

so having a lower minSdk than 26 will not cause an issue ?

不,既不是 minSdk 也不是 targetSdk 真的很重要。重要的是 compileSdk ,必须 26 或更高。

No, neither minSdk nor targetSdk really matter. What matters is compileSdk which must be 26 or higher.

这篇关于Android O转换为findViewById不再需要了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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