解决Android Studio报的警告:Resource IDs will be non-final in Android Gradle Plugin version 5.0

it2026-08-11  7

升级Android studio4.1后用switch语句判断id直接给出了警告

Resource IDs will be non-final in Android Gradle Plugin version 5.0, avoid using them in switch case statements

int id = view.getId(); switch (id) { case R.id.button1: action1(); break; case R.id.button2: action2(); break; case R.id.button3: action3(); break; }

谷歌官方给出的解决办法是把switch换成if else

int id = view.getId(); if (id == R.id.button1) { action1(); } else if (id == R.id.button2) { action2(); } else if (id == R.id.button3) { action3(); }

 

根据谷歌官方的说法,这样的写法变更在UI中几乎没有性能损失。

 

最新回复(0)