Button buttonOk = (Button) findViewById(R.id.button1);
buttonOk.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
DialogUtil.showCustomMessageOK(MainActivity.this, "Confirmation", "Just click OK to continue !");
}
});
Button buttonOkCancel = (Button) findViewById(R.id.button2);
buttonOkCancel.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
DialogUtil.showCustomMessage(MainActivity.this, "Alert", "Are you sure you want continue?");
}
});
/** * 含取消和确定按钮的对话框 * * @param context * @param pTitle * @param pMsg */
public static void showCustomMessage(Context context, String pTitle, final String pMsg) {
final Dialog lDialog = new Dialog(context, R.style.alert_dialog);
// 设置窗口遮罩,点击警告框之外的部分对话框不消失
lDialog.setCanceledOnTouchOutside(false);
// 设置不可以用“返回键”取消
lDialog.setCancelable(false);
// 设置窗口默认不显示标题栏
lDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
lDialog.setContentView(R.layout.ok_cancel_activity);
((TextView) lDialog.findViewById(R.id.dialog_title)).setText(pTitle);
((TextView) lDialog.findViewById(R.id.dialog_message)).setText(pMsg);
((Button) lDialog.findViewById(R.id.cancel))
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
lDialog.dismiss();
}
});
((Button) lDialog.findViewById(R.id.ok)).setText("确 定");
((Button) lDialog.findViewById(R.id.ok))
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
lDialog.dismiss();
}
});
lDialog.show();
}
/** * it will show the OK dialog like iphone, make sure no keyboard is visible * * @param pTitle title for dialog * @param pMsg msg for body */
public static void showCustomMessageOK(Context context, String pTitle, final String pMsg) {
final Dialog lDialog = new Dialog(context, R.style.alert_dialog);
// 设置窗口遮罩,点击警告框之外的部分对话框不消失
lDialog.setCanceledOnTouchOutside(false);
// 设置不可以用“返回键”取消
lDialog.setCancelable(false);
// 设置窗口默认不显示标题栏
lDialog.setContentView(R.layout.simple_ok_activity);
((TextView) lDialog.findViewById(R.id.dialog_title)).setText(pTitle);
((TextView) lDialog.findViewById(R.id.dialog_message)).setText(pMsg);
((Button) lDialog.findViewById(R.id.ok)).setText("确 定");
((Button) lDialog.findViewById(R.id.ok))
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
lDialog.dismiss();
}
});
lDialog.show();
}
<!-- 自定义alert dialog -->
<style name="alert_dialog" parent="android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@drawable/corners_white_bg</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal|center_vertical">
<LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:layout_marginLeft="20dp" android:layout_marginRight="20dp">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:textColor="@color/black" android:text="About to call 323" android:textSize="18sp" android:id="@+id/dialog_title" />
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" android:gravity="center_horizontal" android:textSize="16sp" android:textColor="@color/black" android:text="Are you sure you want to proceed?" android:id="@+id/dialog_message" />
<Button android:layout_width="match_parent" android:layout_height="40dp" android:background="@drawable/login_btn_select_bg" android:layout_margin="10dp" android:text="确定" android:textSize="16dp" android:id="@+id/ok" />
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal|center_vertical">
<LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:layout_marginLeft="20dp" android:layout_marginRight="20dp">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="17dp" android:textColor="@color/black" android:text="About to call 323" android:textSize="18sp" android:id="@+id/dialog_title" />
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" android:gravity="center_horizontal" android:textSize="16sp" android:textColor="@color/black" android:text="Are you sure you want to proceed?" android:id="@+id/dialog_message" />
<LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:layout_marginBottom="10dp" android:gravity="center_horizontal">
<Button android:layout_height="40dp" android:layout_width="0dp" android:background="@drawable/login_btn_select_bg" android:layout_weight="0.5" android:layout_gravity="left" android:textColor="@color/black" android:text="取 消" android:layout_marginLeft="10dp" android:id="@+id/cancel" />
<Button android:layout_height="40dp" android:layout_width="0dp" android:background="@drawable/login_btn_select_bg" android:text="确 定" android:layout_weight="0.5" android:layout_marginLeft="8dp" android:textColor="@color/black" android:layout_marginRight="10dp" android:layout_marginBottom="10dp" android:id="@+id/ok" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
放在drawable文件夹下,没有的话新建。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF" />
<stroke android:width="0.01dp" android:color="#e3e3e3" />
<corners android:topLeftRadius="10dp" android:topRightRadius="10dp" android:bottomRightRadius="10dp" android:bottomLeftRadius="10dp" />
</shape>
这样警告框1,2 基本上就完成了,下篇文章中将介绍一下进度条自定义dialog的实现!