亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

訪問 TimePickerDialog 的 NumberPicker 對象而不是

訪問 TimePickerDialog 的 NumberPicker 對象而不是

阿晨1998 2023-10-13 09:35:16
我正在嘗試創建一個具有小時和分鐘的自定義間隔的自定義,并設法通過使用此問題的TimePickerDialog答案來解決它。問題是 Android Studio 顯示以下警告:不支持通過反射訪問內部 API,并且可能無法在所有設備上或將來工作。使用反射訪問隱藏/私有的 Android API 并不安全;它通常無法在其他供應商的設備上運行,并且可能會突然停止工作[...]問題在第一行: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對象的替代方法。
查看完整描述

1 回答

?
holdtom

TA貢獻1805條經驗 獲得超10個贊

我通過使用找到了解決方案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"

));


查看完整回答
反對 回復 2023-10-13
  • 1 回答
  • 0 關注
  • 120 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號