自定義AlertDialog消息提示框,大家根據我的代碼自行擴充。下圖是威哥做的demo消息提示效果圖:
威哥不喜歡廢話,直接上代碼。
1、消息提示java類:
package com.msh.mshselfweighing.utils;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import com.msh.mshselfweighing.R;
/**
* 消息提示幫助類
* Author:William(徐威)
* Create Time:2018-09-13
*/
public class MessageBox {
private static Context mContext; //操作頁面
private static AlertDialog alertDialog; //彈框對象
private static MessageBox box;
/**
* 初始化消息幫助類
* Author:William(徐威)
* Create Time:2018-09-13
*
* @param activityContent
*/
public static MessageBox init(Context activityContent) {
mContext = activityContent;
if (box == null) {
box = new MessageBox();
}
return box;
}
/**
* 打開資訊提示框
* Author:William(徐威)
* Create Time:2018-09-13
*
* @param strMsg
*/
public void showInfoMsg(String strMsg) {
try {
AlertDialog.Builder builder = new AlertDialog.Builder(mContext); //建立AlertDialog的構造器的對象
builder.setTitle("妙生活資訊提示"); //設定構造器标題
builder.setIcon(R.mipmap.ic_launcher); //構造器對應的圖示
builder.setMessage(strMsg); //構造器内容,為對話框設定文本項(之後還有清單項的例子)
//為構造器設定關閉按鈕
builder.setPositiveButton("關 閉", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
//利用構造器建立AlertDialog的對象,實作執行個體化
alertDialog = builder.create();
alertDialog.show();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
2、頁面調用:
MessageBox.init(this).showInfoMsg("威哥威武。");