Skip to content

Instantly share code, notes, and snippets.

@Slowhand0309
Created July 27, 2019 04:50
Show Gist options
  • Save Slowhand0309/a4df4a8613ba6a60b920453e1bd96643 to your computer and use it in GitHub Desktop.
Save Slowhand0309/a4df4a8613ba6a60b920453e1bd96643 to your computer and use it in GitHub Desktop.
[Kotlin Extension Context] #Kotlin #Android
/**
* Toastの簡易版
*
* @param text 表示文字列
* @param duration デフォルト: Toast.LENGTH_LONG
* @return {@link Toast}
*/
@Suppress("NOTHING_TO_INLINE")
inline fun Context.toast(text: CharSequence, duration: Int = Toast.LENGTH_LONG): Toast {
return Toast.makeText(this, text, duration).also { it.show() }
}
/**
* Toastの簡易版
*
* @param resId 文字列ID
* @param duration デフォルト: Toast.LENGTH_LONG
* @return {@link Toast}
*/
@Suppress("NOTHING_TO_INLINE")
inline fun Context.toast(@StringRes resId: Int, duration: Int = Toast.LENGTH_LONG): Toast {
return Toast.makeText(this, resId, duration).also { it.show() }
/**
* AlertDialogの簡易版
*
* @param text 表示メッセージ
* @param onPositive okボタンクリック時
*/
fun Context.alert(text: CharSequence, onPositive: () -> Unit): AlertDialog
= AlertDialog.Builder(this)
.setMessage(text)
.setPositiveButton("ok") { _, _ ->
onPositive()
}
.setNegativeButton("cancel") { dialog, _ ->
dialog.dismiss()
}
.show()
/**
* AlertDialog表示
*
* @param resId 表示メッセージ文字列ID
* @param onPositive okボタンクリック時
*/
fun Context.alert(@StringRes resId: Int, onPositive: () -> Unit): AlertDialog
= AlertDialog.Builder(this)
.setMessage(resId)
.setPositiveButton("ok") { _, _ ->
onPositive()
}
.setNegativeButton("cancel") { dialog, _ ->
dialog.dismiss()
}
.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment