封装自定义的Dialog和Toast

 Dialog与Toast封装

封装一种自定义的AlertDialog 和 Toast.

public class CustomDialog {
private AlertDialog.Builder builder;
private Context context;
public CustomDialog(Context context) {
<span style="white-space:pre">	</span>this.context = context;
}
public void createDialog(String buttontext, String title, String message,
<span style="white-space:pre">		</span>final CallBack callBack) {
<span style="white-space:pre">	</span>builder = new AlertDialog.Builder(context);
<span style="white-space:pre">	</span>builder.setTitle(title);
<span style="white-space:pre">	</span>builder.setMessage(message);
<span style="white-space:pre">	</span>builder.setPositiveButton(buttontext, new OnClickListener() {
<span style="white-space:pre">	</span>public void onClick(DialogInterface arg0, int arg1) {
<span style="white-space:pre">		</span>callBack.isConfirm(true);
<span style="white-space:pre">		</span>}
<span style="white-space:pre">	</span>});
<span style="white-space:pre">	</span>builder.create().show();
}
public interface CallBack {
<span style="white-space:pre">	</span>public void isConfirm(boolean flag);
}
public void createToasts(String message,LayoutInflater layoutInflater) {
<span style="white-space:pre">	</span>// Toast.makeText(context, message, Toast.LENGTH_LONG).show();
    View view = layoutInflater.inflate(R.layout.toast, null);
    TextView textView = (TextView)view.findViewById(R.id.text);
    textView.setText(message);    
    Toast toast = new Toast(context);
    toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
    toast.setDuration(Toast.LENGTH_LONG);
    toast.setView(view);
    toast.show();
<span style="white-space:pre">	</span>}
}
这里定义了一个内部接口,接口定义了一个isConfirm方法,含有一个 flag 变量,用来记录是否按下 确定按钮。
在MainActivity中调用它们:

<span style="font-family:Microsoft YaHei;">button.setOnClickListener(new View.OnClickListener() {
	public void onClick(View arg0) {
	CustomDialog dialog = new CustomDialog(MainActivity.this);
	dialog.createDialog("确定", "提示", "您确定要删除吗?", new CallBack() {
		public void isConfirm(boolean flag) {
			System.out.println("----->>" + flag);
			if (flag) {
				//dosomething.....判断执行业务逻辑
			}
		}
	});
	}
});
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
        CustomDialog dialog = new CustomDialog(MainActivity.this);
        dialog.createToasts("网络有有异常!!",getLayoutInflater());
        
		}
	});
}

<span style="font-family: 'Microsoft YaHei'; font-size: 14px; line-height: 26px;"> Android的对话框有两种:PopupWindow和AlertDialog。</span><br style="font-family: 'Microsoft YaHei'; font-size: 14px; line-height: 26px;" /><span style="font-family: 'Microsoft YaHei'; font-size: 14px; line-height: 26px;"> </span><br style="font-family: 'Microsoft YaHei'; font-size: 14px; line-height: 26px;" /><span style="font-family: 'Microsoft YaHei'; font-size: 14px; line-height: 26px;">它们的不同点在于:AlertDialog的位置固定,而PopupWindow的位置可以随意</span><br style="font-family: 'Microsoft YaHei'; font-size: 14px; line-height: 26px;" /><span style="font-family: 'Microsoft YaHei'; font-size: 14px; line-height: 26px;">AlertDialog是非阻塞线程的,AlertDialog弹出的时候,后台可是还可以做其他事情的哦。 </span><br style="font-family: 'Microsoft YaHei'; font-size: 14px; line-height: 26px;" /><span style="font-family: 'Microsoft YaHei'; font-size: 14px; line-height: 26px;">而PopupWindow是阻塞线程的, 这就意味着在我们退出这个弹出框之前,程序会一直等待</span><br style="font-family: 'Microsoft YaHei'; font-size: 14px; line-height: 26px;" /><br style="font-family: 'Microsoft YaHei'; font-size: 14px; line-height: 26px;" /><span style="font-family: 'Microsoft YaHei'; font-size: 14px; line-height: 26px;">PopupWindow的位置按照有无偏移分,可以分为偏移和无偏移两种;按照参照物的不同,可以分为相对于某个控件(Anchor锚)和相对于父控件。具体如下</span><br style="font-family: 'Microsoft YaHei'; font-size: 14px; line-height: 26px;" /><span style="font-family: 'Microsoft YaHei'; font-size: 14px; line-height: 26px;">showAsDropDown(View anchor):相对某个控件的位置(正左下方),无偏移</span><br style="font-family: 'Microsoft YaHei'; font-size: 14px; line-height: 26px;" /><span style="font-family: 'Microsoft YaHei'; font-size: 14px; line-height: 26px;">showAsDropDown(View anchor, int xoff, int yoff):相对某个控件的位置,有偏移</span><br style="font-family: 'Microsoft YaHei'; font-size: 14px; line-height: 26px;" /><span style="font-family: 'Microsoft YaHei'; font-size: 14px; line-height: 26px;">showAtLocation(View parent, int gravity, int x, int y):相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移 </span><br style="font-family: 'Microsoft YaHei'; font-size: 14px; line-height: 26px;" /><span style="font-family: 'Microsoft YaHei'; font-size: 14px; line-height: 26px;">LayoutInflater layoutInflater = LayoutInflater.from(this);</span><br style="font-family: 'Microsoft YaHei'; font-size: 14px; line-height: 26px;" /><span style="font-family: 'Microsoft YaHei'; font-size: 14px; line-height: 26px;">View popupWindow = layoutInflater.inflate(R.layout.popup, null);</span>

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值