写了一个简单的确认取消的Popupwindow,可以设置图标,标题,加入了确认、取消的点击事件回调。
啥也不说了,先上效果图:
giphy (1).gif
完整代码:
public class ComfirPopupwindow extends PopupWindow{
private TextView tvTitle,tvSure,tvCancel;
private ImageView ivIcon;
public ComfirPopupwindow(Context context, ICallListener confirmListener){
init(context);
setListener(confirmListener);
}
public ComfirPopupwindow(Context context, ICallListener confirmListener, ICallListener cancelListener){
init(context);
setListener(confirmListener,cancelListener);
}
public void init(Context context){
View view = LayoutInflater.from(context).inflate(R.layout.dialog_upload_file,null);
setFocusable(true);
setOutsideTouchable(true);
setBackgroundDrawable(new BitmapDrawable());
setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
setContentView(view);
tvTitle = (TextView) view.findViewById(R.id.tv_dialog_title);
ivIcon = (ImageView) view.findViewById(R.id.tv_content);
tvSure = (TextView) view.findViewById(R.id.tv_sureadd);
tvCancel = (TextView) view.findViewById(R.id.tv_canceladd);
}
/**
* 仅带确认回调
* @param listener
*/
private void setListener(final ICallListener listener){
tvSure.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
listener.callBack();
}
});
tvCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
}
/**
* 同时带有确认和取消回调
* @param listener
* @param listener2
*/
private void setListener(final ICallListener listener,final ICallListener listener2){
tvSure.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
listener.callBack();
}
});
tvCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
listener2.callBack();
}
});
}
public void setTitle(String title){
tvTitle.setText(title);
}
public void setIcon(int resId){
ivIcon.setImageResource(resId);
}
public interface ICallListener {
void callBack();
}
}
使用示例:
/**
* 示例1
* @param view
*/
public void click(View view){
ComfirPopupwindow comfirPopupwindow = new ComfirPopupwindow(this, new ComfirPopupwindow.ICallListener() {
@Override
public void callBack() {
Toast.makeText(getApplicationContext(),"点击确认",Toast.LENGTH_SHORT).show();
}
});
comfirPopupwindow.showAtLocation(rootView, Gravity.CENTER,0,0);
comfirPopupwindow.setTitle("是否开始下载?");
comfirPopupwindow.setIcon(R.mipmap.ic);
}
/**
* 示例2
* @param view
*/
public void clicktwo(View view){
ComfirPopupwindow comfirPopupwindow = new ComfirPopupwindow(this, new ComfirPopupwindow.ICallListener() {
@Override
public void callBack() {
Toast.makeText(getApplicationContext(), "点击确认", Toast.LENGTH_SHORT).show();
}
}, new ComfirPopupwindow.ICallListener() {
@Override
public void callBack() {
Toast.makeText(getApplicationContext(), "点击取消", Toast.LENGTH_SHORT).show();
}
});
comfirPopupwindow.showAtLocation(rootView, Gravity.CENTER,0,0);
comfirPopupwindow.setTitle("是否取消该操作");
comfirPopupwindow.setIcon(R.mipmap.icon_avatar);
}
网友评论