Dialog与Toast封装
在MainActivity中调用它们:
封装一种自定义的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>