-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
增加常用的两种Dialog使用,简化代码 保留小数与转换DP Toast
- Loading branch information
Showing
7 changed files
with
276 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package lhl.kotlinextends | ||
|
||
import com.blankj.utilcode.util.ConvertUtils | ||
|
||
private var defaultNumber = 2 | ||
fun setDefaultNumber(num: Int) { | ||
defaultNumber = num | ||
} | ||
|
||
/** | ||
* 保留defaultNumber位小数 | ||
*/ | ||
fun Double.kp(num: Int = defaultNumber): String { | ||
return String.format("%.${num}f", this) | ||
} | ||
|
||
fun Float.kp(num: Int = defaultNumber): String { | ||
return String.format("%.${num}f", this) | ||
} | ||
|
||
fun Int.dp(): Int = this.toFloat().dp() | ||
fun Double.dp(): Int = this.toFloat().dp() | ||
fun Float.dp(): Int = ConvertUtils.px2dp(this) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package lhl.kotlinextends | ||
|
||
import android.app.Activity | ||
import android.app.ProgressDialog | ||
|
||
class PDialog(activity: Activity, message: String, cancel: Int, dismissListener: DialogListener? = null) { | ||
|
||
constructor(activity: Activity, message: String) : this(activity, message, CANCEL_NO) | ||
|
||
constructor(activity: Activity, message: String, dismissListener: DialogListener) : this(activity, message, CANCEL_BACK, dismissListener) | ||
|
||
private var progressDialog = ProgressDialog(activity) | ||
|
||
init { | ||
progressDialog.setMessage(message) | ||
when (cancel) { | ||
CANCEL_ALL -> { | ||
progressDialog.setCancelable(true) | ||
progressDialog.setCanceledOnTouchOutside(true) | ||
} | ||
CANCEL_BACK -> { | ||
progressDialog.setCancelable(true) | ||
progressDialog.setCanceledOnTouchOutside(false) | ||
} | ||
CANCEL_NO -> { | ||
progressDialog.setCancelable(false) | ||
progressDialog.setCanceledOnTouchOutside(false) | ||
} | ||
} | ||
progressDialog.setOnDismissListener { dismissListener?.invoke() } | ||
} | ||
|
||
|
||
fun setMessage(text: String) { | ||
progressDialog.setMessage(text) | ||
} | ||
|
||
fun show() { | ||
progressDialog.show() | ||
} | ||
|
||
fun dismiss() { | ||
progressDialog.dismiss() | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package lhl.kotlinextends | ||
|
||
import android.app.Activity | ||
import android.app.AlertDialog | ||
import android.app.Dialog | ||
|
||
typealias DialogListener = () -> Unit | ||
|
||
const val CANCEL_ALL = 0 | ||
const val CANCEL_BACK = 1 | ||
const val CANCEL_NO = 2 | ||
|
||
class TDialog(activity: Activity, message: String = "", private val cancel: Int = CANCEL_ALL, positive: DialogListener? = null) { | ||
|
||
private var builder: AlertDialog.Builder = AlertDialog.Builder(activity) | ||
private var dialog: Dialog? = null | ||
|
||
init { | ||
builder.setTitle("提示") | ||
builder.setMessage(message) | ||
builder.setPositiveButton("确定", null) | ||
if (cancel == CANCEL_NO) | ||
builder.setCancelable(false) | ||
positive?.let { setPositiveButton(listener = it) } | ||
} | ||
|
||
fun show() { | ||
dialog = builder.create() | ||
if (cancel != CANCEL_ALL) | ||
dialog?.setCanceledOnTouchOutside(false) | ||
dialog?.show() | ||
} | ||
|
||
fun dismiss() { | ||
dialog?.dismiss() | ||
} | ||
|
||
fun setPositiveButton(text: String = "确定", listener: DialogListener? = null) { | ||
builder.setPositiveButton(text) { _, _ -> listener?.invoke() } | ||
show() | ||
} | ||
|
||
fun setNegativeButton(text: String = "取消", listener: DialogListener? = null) { | ||
builder.setNegativeButton(text) { _, _ -> listener?.invoke() } | ||
show() | ||
} | ||
|
||
fun setButtons(textPositive: String = "确定", textNegative: String = "取消", positive: DialogListener? = null, negative: DialogListener? = null) { | ||
builder.setPositiveButton(textPositive) { _, _ -> positive?.invoke() } | ||
builder.setNegativeButton(textNegative) { _, _ -> negative?.invoke() } | ||
show() | ||
} | ||
|
||
fun setNeutralButton(text: String, listener: DialogListener? = null) { | ||
builder.setNeutralButton(text) { _, _ -> listener?.invoke() } | ||
} | ||
|
||
fun setTitle(text: String) { | ||
builder.setTitle(text) | ||
} | ||
|
||
fun setMessage(text: String) { | ||
builder.setMessage(text) | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
package lhl.kotlinextends | ||
|
||
import com.blankj.utilcode.util.ToastUtils | ||
import com.blankj.utilcode.util.Utils | ||
|
||
fun String.toast(){ | ||
fun String.toast() { | ||
ToastUtils.showShort(this) | ||
} | ||
|
||
fun String.longToast() { | ||
ToastUtils.showLong(this) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.