custom timepicker에서 timeinterval을 사용하는 과정에서 최신기기에서는 사용되지 않는 코드 수정

울늘·2022년 6월 1일
0

이전 코드

참고하였던 tistory 블로그

위의 블로그에서 사용된

    @SuppressLint("PrivateApi")
    fun setInterval(
        @IntRange(from = 1, to = 30)
        timeInterval: Int = defaultInterval
    ) {
        try {
            val classForId = Class.forName("com.android.internal.R\$id")
            val fieldId = classForId.getField("minute").getInt(null)
            (this.findViewById(fieldId) as NumberPicker).apply {
                minValue = DateTimeUtil.MINUTES_MIN
                maxValue = DateTimeUtil.MINUTES_MAX / timeInterval - 1
                displayedValues = getDisplayedValue()
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

해당 코드에서
try문 부분을 이와 같이 수정한다.

		try {
            (this.findViewById(
                Resources.getSystem().getIdentifier(
                "minute",
                "id",
                "android"
            )) as NumberPicker).apply {
                minValue = resources.getInteger(R.integer.minutes_min)
                maxValue = resources.getInteger(R.integer.minutes_max) / timeInterval - 1
                displayedValues = getDisplayedValue()
            }

스택오버플로우 참고

profile
주니어 안드로이드 개발자입니다!

0개의 댓글