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>