Shouheng88 / utils-android

Convenient android utils and Kotlin DSL styled extensions.
Apache License 2.0
120 stars 31 forks source link

快捷方法调整 #21

Closed Shouheng88 closed 11 months ago

Shouheng88 commented 11 months ago

/Users/shouhwang/.gradle/caches/modules-2/files-2.1/com.github.Shouheng88/utils-ktx/2.8.7/3d16a937b3877b2a49b6d05a9c49cb4667b181fa/utils-ktx-2.8.7-sources.jar!/me/shouheng/utils/ktx/StringKtx.kt:67

fun String.safeToInt(str: String, def: Int = 0): Int = try { str.toInt() } catch (e: NumberFormatException) { def }

fun String.safeToLong(str: String, def: Long = 0): Long = try { str.toLong() } catch (e: NumberFormatException) { def }

fun String.safeToFloat(str: String, def: Float = 0f): Float = try { str.toFloat() } catch (e: NumberFormatException) { def }

fun String.safeToDouble(str: String, def: Double = .0): Double = try { str.toDouble() } catch (e: NumberFormatException) { def }

写错了,应该用 this 就行

Shouheng88 commented 11 months ago

方法错误

me.shouheng.utils.ui.ViewUtils#setIconsVisible

public static void setIconsVisible(Menu menu, boolean visible) {
        if (menu != null) {
            try {
                Method method = Menu.class.getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);
                method.setAccessible(true);
                method.invoke(menu, visible);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

setOptionalIconsVisible 是 Menu 的子类 MenuBuilder 的方法

改为,

fun setIconsVisible(menu: Menu?, visible: Boolean) {
    if (menu != null) {
        try {
            val method =
                menu.javaClass.getDeclaredMethod(
                    "setOptionalIconsVisible",
                    java.lang.Boolean.TYPE
                )
            method.isAccessible = true
            method.invoke(menu, visible)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}
Shouheng88 commented 11 months ago

me.shouheng.utils.store.KV#setOnSharedPreferenceChangeListener

这个方法应该改名为 registerOnSharedPreferenceChangeListener