1. 程式人生 > 其它 >直播系統原始碼,簡易的自定義確認彈框AlertDialog

直播系統原始碼,簡易的自定義確認彈框AlertDialog

直播系統原始碼,簡易的自定義確認彈框AlertDialog實現的相關程式碼


CustomDialog(this, "清空輸入", "確定要清空已輸入內容嗎?", object : CustomDialog.ClickCallBack {
override fun onYesClick(dialog: CustomDialog) {
//點選確認按鈕後具體操作
dialog.dismiss()
}

}).show()

自定義Dialog程式碼:


/**
* Created by Xinghai.Zhao
* 自定義選擇彈框
*/
@SuppressLint("InflateParams")
class CustomDialog(context: Context?) : AlertDialog(context){
var mCallBack: ClickCallBack? = null
var mTextViewTitle: TextView? = null
var mTextViewContent: TextView? = null
constructor(context: Context?, title: String?, content: String?, callBack: ClickCallBack) : this(context) {
mCallBack = callBack
if (title != null) mTextViewTitle?.text = title
if (content != null) mTextViewContent?.text = content
}
init {
val inflate = LayoutInflater.from(context).inflate(R.layout.dialog_custom, null)
setView(inflate)
//設定點選別的區域不關閉頁面
setCancelable(false)
mTextViewTitle = inflate.findViewById(R.id.dialog_custom_title)
mTextViewContent = inflate.findViewById(R.id.dialog_custom_content)
inflate.findViewById<View>(R.id.dialog_custom_yes).setOnClickListener{mCallBack?.onYesClick(this)}
inflate.findViewById<View>(R.id.dialog_custom_no).setOnClickListener{dismiss()}
}
interface ClickCallBack {
fun onYesClick(dialog:CustomDialog)
}
}

佈局檔案:dialog_custom


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/white_circle_background10"
android:orientation="vertical">

<TextView
android:id="@+id/dialog_custom_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="20dp"
android:textColor="@color/TextBlack"
android:textSize="@dimen/TextSizeTitle" />

<TextView
android:id="@+id/dialog_custom_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingBottom="20dp"
android:textColor="@color/TextGray"
android:textSize="@dimen/TextSizeContent" />

<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/LightGrayStill" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:id="@+id/dialog_custom_no"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:padding="10dp"
android:text="取消"
android:textColor="@color/TextGray"
android:textSize="@dimen/TextSizeContent" />

<TextView
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="@color/LightGrayStill" />

<TextView
android:id="@+id/dialog_custom_yes"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:padding="10dp"
android:text="確認"
android:textColor="@color/TextGray"
android:textSize="@dimen/TextSizeContent" />


</LinearLayout>

</LinearLayout>

以上就是直播系統原始碼,簡易的自定義確認彈框AlertDialog實現的相關程式碼, 更多內容歡迎關注之後的文章