Class.forName 访问 TimePickerDialog 的 NumberPicker 对象的替代方法

Alternative way to access TimePickerDialog's NumberPicker objects than Class.forName

我正在尝试创建一个自定义 TimePickerDialog 的自定义时间间隔,并使用 this question.

的答案成功解决了这个问题

问题是 Android Studio 显示以下警告:

Accessing internal APIs via reflection is not supported and may not work on all devices, or in the future. Using reflection to access hidden/private Android APIs is not safe; it will often not work on devices from other vendors, and it may suddenly stop working[...]

问题在第一行:

Class<?> classForid = Class.forName("com.android.internal.R$id");

Field timePickerField = classForid.getField("timePicker");
TimePicker mTimePicker = findViewById(timePickerField.getInt(null));
Field minuteField = classForid.getField("minute");
Field hourField = classForid.getField("hour");
NumberPicker minutePicker = mTimePicker.findViewById(minuteField.getInt(null));
NumberPicker hourPicker = mTimePicker.findViewById(hourField.getInt(null));

我需要一些替代方法来访问这两个 NumberPicker 对象。

我找到了使用 Resources.getSystem().getIdentifier() 的解决方案。

代码现在看起来像这样:

TimePicker timePicker = findViewById(Resources.getSystem().getIdentifier(
    "timePicker", 
    "id", 
    "android"
));

NumberPicker minutePicker = timePicker.findViewById(Resources.getSystem().getIdentifier(
    "minute", 
    "id", 
    "android"
));

NumberPicker hourPicker = timePicker.findViewById(Resources.getSystem().getIdentifier(
    "hour", 
    "id", 
    "android"
));