最近写的一个简易选择框
长这个样子:
有多种模式:
1.输入框模式,有一个标题一个输入框。见上图(构造方法中 isEdit 字段 true的时候是开启输入框)
2.还有一种提示模式,有一个标题一个内容。
扩展:更多模式 比如 一个标题一个内容一个输入框,等。。。
/**
* Created by Xinghai.Zhao on 2020/8/4.
* 自定义选择弹框
*/
class CustomDialog(context: Context?) : AlertDialog(context){
var mCallBack: ClickCallBack? = null;
var mTextViewTitle: TextView? = null;
var mTextViewContent: TextView? = null;
var mYesButton: View? = null;
var mNoButton: View? = null;
var mEditText: EditText? = null;
constructor(context: Context?, title: String?, content: String?, callBack: clickCallBack)
: this(context, title, content, callBack, false) {
}
constructor(context: Context?, title: String?, content: String?, callBack: clickCallBack, isEdit: Boolean) : this(context) {
mCallBack= callBack
if (title != null) mTextViewTitle?.text = title;
if (content != null) mTextViewContent?.text = content;
if (isEdit){
mTextViewContent?.visibility = View.GONE
mEditText?.visibility=View.VISIBLE
//最大输入长度
mEditText?.filters = arrayOf<InputFilter>(InputFilter.LengthFilter(10))
mEditText?.isFocusable = true
mEditText?.isFocusableInTouchMode = true
mEditText?.requestFocus()
mEditText?.keyListener = object : DigitsKeyListener() {
override fun getInputType(): Int {
return InputType.TYPE_TEXT_VARIATION_PASSWORD
}
override fun getAcceptedChars(): CharArray {
//限制文字输入内容
return context?.resources!!.getString(R.string.edit_input)?.toCharArray()
}
}
}
}
init {
val inflate = LayoutInflater.from(context).inflate(R.layout.dialog_custom, null);
setView(inflate)
//设置点击别的区域不关闭页面
setCancelable(false)
mEditText= inflate.findViewById<EditText>(R.id.dialog_custom_edit)
mTextViewTitle = inflate.findViewById<TextView>(R.id.dialog_custom_title)
mTextViewContent = inflate.findViewById<TextView>(R.id.dialog_custom_content)
mYesButton = inflate.findViewById<View>(R.id.dialog_custom_yes)
mNoButton = inflate.findViewById<View>(R.id.dialog_custom_no)
mYesButton?.setOnClickListener{mCallBack?.onYesClick(this)}
mNoButton?.setOnClickListener{dismiss()}
}
interface clickCallBack {
fun onYesClick(dialog:MyDialog)
}
}
Layout文件: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:orientation="vertical"
android:background="@drawable/white_circle_background10">
<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_height="1dp"
android:layout_width="match_parent"
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_weight="1"
android:layout_height="wrap_content"
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_weight="1"
android:layout_height="wrap_content"
android:text="确认"
android:gravity="center"
android:padding="10dp"
android:textColor="@color/TextGray"
android:textSize="@dimen/TextSizeContent" />
</LinearLayout>
</LinearLayout>
外部调用代码
/*
*作者:赵星海
*时间:2020/6/17 11:07
*用途: 输入台牌号
*/
val myDialog =MyDialog(activity,"请输入台牌号","",object :MyDialog.clickCallBack{
override fun yesClick(dialog: MyDialog) {
val input:String =dialog.editText?.text.toString()
if (input==null||input .equals("")) {
Toast.makeText(activity, "台牌号不能为空!", Toast.LENGTH_LONG).show()
} else {
//此处写后续逻辑
dialog?.dismiss()
}
}
},true)
myDialog.setOnDismissListener {
//关闭监听逻辑
}
//显示的位置居中提高100dp-------------
val w = myDialog.window
val lp = w!!.attributes
lp.x = 0
lp.y = -100
//-----------------------------------
myDialog.show();
//显示的宽高根据屏幕百分比调整-----------------------------------
val m = activity!!.windowManager
val d = m.defaultDisplay //为获取屏幕宽、高
val p = myDialog.window!!.attributes //获取对话框当前的参数值
p.height = (d.height * 0.40).toInt() //高度设置为屏幕的0.3
p.width = (d.width * 0.45).toInt() //宽度设置为屏幕的0.5
myDialog.window!!.attributes = p
//--------------------------------------------------------------
下面加分割线的两段代码,其功能我已经写注释咯!
标签:layout,自定义,Kotlin,custom,content,AlertDialog,dialog,android,id From: https://blog.51cto.com/u_13520184/6115485