感觉每次写dialog都很痛苦,虽然能写出来,但每次都要新建一个dialog类,
写里面的布局,
写里面的逻辑,
总感觉很麻烦。
方案核心就是让你只关注布局和以及各种界面上的操作,
dialog本身不处理任何view和数据
更多关于dialog的设置,可以自行扩展
Java和Kotlin版本功能一样
Java版本:
使用:
EasyDialog dialog;
private void useDialog() {
dialog = new EasyDialog(this , R.layout.album_list_item) {
@Override
public void convert(EasyDialog.DialogHolder holder) {
holder.setText(R.id.check_view , "xxxx");
holder.setOnClickListener(R.id.check_view, v -> dialog.dismiss());
}
};
dialog.show();
}
Dialog:
public abstract class EasyDialog extends Dialog {
View layout;
int layoutId;
Context mContext;
public EasyDialog(@NonNull Context context, int mLayoutId) {
super(context, R.style.EasyDialog);
mContext = context;
layoutId = mLayoutId;
initView();
}
protected void initView(){
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout = inflater.inflate(layoutId , null);
addContentView(layout , new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
DialogHolder holder = new DialogHolder(mContext , layout);
convert(holder);
}
public abstract void convert(DialogHolder holder);
public class DialogHolder{
Context context1 ;
View view1;
DialogHolder(Context context , View view){
context1 = context;
view1 = view;
}
public <T extends View> T getView(int viewId) {
View view = view1.findViewById(viewId);
return (T) view;
}
public DialogHolder setText(int viewId, String text) {
TextView tv = getView(viewId);
tv.setText(text);
return this;
}
public DialogHolder setVisiable(int viewId, boolean visiable) {
View view = getView(viewId);
if(visiable){
view.setVisibility(View.VISIBLE);
}else {
view.setVisibility(View.GONE);
}
return this;
}
public DialogHolder setImageResource(int viewId, int resId) {
ImageView view = getView(viewId);
view.setImageResource(resId);
return this;
}
public DialogHolder setImageUrl(int viewId, String url) {
ImageView view = getView(viewId);
Glide.with(mContext).load(url).into(view);
return this;
}
public DialogHolder setOnClickListener(int viewId, View.OnClickListener listener) {
View view = getView(viewId);
view.setOnClickListener(listener);
return this;
}
public DialogHolder setTextCorlor(int viewId, int colorId) {
TextView view = getView(viewId);
view.setTextColor(ContextCompat.getColor(mContext,colorId));
return this;
}
public DialogHolder setBackgroundCorlor(int viewId, int colorId) {
View view = getView(viewId);
view.setBackgroundColor(ContextCompat.getColor(mContext,colorId));
return this;
}
public DialogHolder setAlpha(float alpha) {
view1.setAlpha(alpha);
return this;
}
}
}
KT版
abstract class EasyDialog : Dialog {
var layout: View? = null
var layoutId: Int = 0
var mContext: Context
constructor(mContext: Context, mLayoutId: Int) : super(mContext, R.style.EasyDialog) {
this.mContext = mContext;
this.layoutId = mLayoutId;
initView();
}
private fun initView() {
val infalater: LayoutInflater = mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
layout = infalater.inflate(layoutId , null);
this.addContentView(layout , ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT))
val holder = DialogHolder(context , layout);
convert(holder);
}
abstract fun convert(holder: DialogHolder)
class DialogHolder(context: Context , view:View?){
var mContext: Context? = context
var view: View? = view
fun <T : View?> getView(viewId: Int): T {
var view: View? = view?.findViewById(viewId)?:null
return view as T
}
fun setText(viewId: Int , text:String) : DialogHolder{
var tv = getView<TextView>(viewId)
tv.text = text?:""
return this
}
fun setTextCorlor(viewId: Int, colorId: Int): DialogHolder? {
val view = getView<TextView>(viewId)
view.setTextColor(ContextCompat.getColor(mContext!!, colorId))
return this
}
fun setVisiable(viewId: Int , mVisiable:Boolean) : DialogHolder{
val view = getView<View>(viewId)
if (mVisiable) {
view.visibility = View.VISIBLE
} else {
view.visibility = View.GONE
}
return this
}
fun setImageUrl(viewId: Int , url :String):DialogHolder{
val iv = getView<ImageView>(viewId)
Glide.with(mContext!!).load(url).into(iv)
return this;
}
fun setImageResource(viewId: Int , resId :Int):DialogHolder{
val iv = getView<ImageView>(viewId)
Glide.with(mContext!!).load(resId).into(iv)
return this;
}
fun setOnClickListener(viewId: Int , listener:View.OnClickListener):DialogHolder{
val view = getView<View>(viewId)
view.setOnClickListener(listener)
return this
}
fun setBackgroundCorlor(viewId: Int, colorId: Int): DialogHolder? {
val view = getView<View>(viewId)
view.setBackgroundColor(ContextCompat.getColor(mContext!!, colorId))
return this
}
fun setAlpha(alpha: Float): DialogHolder? {
view?.alpha = alpha
return this
}
}
}
使用:
var commonDialog : EasyDialog? = null
private fun initDialog() {
var show = true;
if(show){
commonDialog = object :EasyDialog(this , R.layout.agree_dialog){
override fun convert(holder: DialogHolder) {
holder.setOnClickListener(R.id.negtive , View.OnClickListener {
Toast.makeText(this@MainActivity , "点击了取消" , Toast.LENGTH_SHORT).show()
AppUtils.exitApp()
})
holder.setOnClickListener(R.id.positive , View.OnClickListener {
Toast.makeText(this@MainActivity , "点击了确定" , Toast.LENGTH_SHORT).show()
commonDialog?.dismiss()
requestSomePerMissions()
})
}
}
commonDialog?.show();
}
}
style:
<style name="EasyDialog" parent="android:style/Theme.Dialog">
<!--背景颜色及和透明程度-->
<item name="android:windowBackground">@android:color/transparent</item>
<!--是否去除标题 -->
<item name="android:windowNoTitle">true</item>
<!--是否去除边框-->
<item name="android:windowFrame">@null</item>
<!--是否浮现在activity之上-->
<item name="android:windowIsFloating">true</item>
<!--是否模糊-->
<item name="android:backgroundDimEnabled">true</item>
</style>