本框架是仿照AlterDialog的建造者設(shè)計(jì)模式的寫法。比原生的dialog使用起來更靈性蛹含,通俗易懂旱捧。
這里會比原生的AlterDialog多一個DialogViewHelper作用是設(shè)置View的一些屬性和狀態(tài)恰画,下面附上代碼
package com.example.chao.library;
import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import java.lang.ref.WeakReference;
/**
* Created by yang2 on 2017/8/11.
*/
class DialogViewHelper {
private View mContentView = null;
private SparseArray<WeakReference<View>> mViews;
public DialogViewHelper(Context context,int mViewLayoutResId){
this();
this.mContentView = LayoutInflater.from(context).inflate(mViewLayoutResId,null);
}
public DialogViewHelper(){
mViews = new SparseArray<>();
}
public void setContentView(View contentView){
this.mContentView = contentView;
}
public void setText(int viewId,CharSequence charSequence){
TextView textView = getView(viewId);
assert textView!=null;
textView.setText(charSequence);
}
public <T extends View>T getView(int viewId) {
WeakReference<View> viewWeakReference = mViews.get(viewId);
View view = null;
if (viewWeakReference!=null){
view = viewWeakReference.get();
}else {
view = mContentView.findViewById(viewId);
mViews.put(viewId,new WeakReference<View>(view
dz));
}
return (T) view;
}
public void setOnClickListener(int viewId, View.OnClickListener listener){
View view = getView(viewId);
if (view!=null){
view.setOnClickListener(listener);
}
}
public View getContentView() {
return mContentView;
}
}
使用說明
baseDialog = new BaseDialog.Builder(this, R.layout.dialog_item)
.setText(R.id.tv_title,"標(biāo)題")
.setText(R.id.tv_confirm,"確認(rèn)")
.setText(R.id.tv_cancle,"取消")
.setOnclickListener(R.id.tv_confirm, new View.OnClickListener() {
@Override
public void onClick(View view) {
baseDialog.dismiss();
}
})
.setOnclickListener(R.id.tv_confirm, new View.OnClickListener() {
@Override
public void onClick(View view) {
baseDialog.dismiss();
}
})
//點(diǎn)擊外部是否銷毀
.setCancelable(false)
//全屏
.fullWidth()
.show();