RadioGroup.clearCheck() 之后的 getCheckedRadioButtonId() return 应该做什么?

What should getCheckedRadioButtonId() return after RadioGroup.clearCheck()?

来自docs

public void clearCheck ()

Clears the selection. When the selection is cleared, no radio button in this group is selected and getCheckedRadioButtonId() returns null.

但看起来确实如此 returns -1。此外,行:

if(radioGroup.getCheckedRadioButtonId()==null)

报错:

The operator == is undefined for the argument type(s) int, null

我应该指望它在执行 clearCheck() 后返回 -1 吗?

radioGroup.getCheckedRadioButtonId() 的 return 类型(如文档中所示)是 int。一个int不可能是null,所以我觉得你应该依赖-1.

编辑:如果您不确定 Android 组件的工作原理,只需检查源代码 :) 对于 RadioGroup,请参阅示例:http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/android/widget/RadioGroup.java#RadioGroup.clearCheck%28%29