安卓自定義超級提示框Dialog

源碼下載地址:http://download.csdn.net/detail/haiyangzhisheng/9504180

話題來源:爲新的項目添加原來的項目做的提示框時,要把原來項目的自定義Dialog複製過來,整理了一下,需要提示框時只需要複製Java類和一個佈局文件和動畫文件就行了

 

使用方式:

final AAMyAlertDialogmDialog = new AAMyAlertDialog(this);

mDialog.setTitle("我的提示");

mDialog.setMessage("提示內容");

mDialog.setPositiveButton("確定",new OnClickListener() {

 

@Override

public void onClick(Viewv) {

mDialog.dismiss();

}

});

mDialog.setNegativeButton("取消",new OnClickListener() {

 

@Override

public void onClick(Viewv) {

}

});

mDialog.setMidleButton("操作",new OnClickListener() {

 

@Override

public void onClick(Viewv) {

}

});

mDialog.show();

 

實現原理:

1、繼承Dialog

2、GradientDrawable實現圓角背景

3、StateListDrawable實現點擊效果

4、在onAttachedToWindow方法中設置彈出框在手機中佔比

5、Xml中定義AlphaAnimationScaleAnimation等動畫類實現動畫效果

 

實現代碼解析:

AAMyAlertDialog.java文件

 

public class AAMyAlertDialogextends Dialog {

/**

 * 提示框的三個按鈕,自動顯示和隱藏

 */

private Button btn_ok, btn_cancle, btn_midle;

/**

 * 提示內容

 */

private TextView tv_title, tv_alert, tv_alertred;

/**

 * 標題分割線

 */

private ImageView iv_title_line;

/**

 * 提示內容變量初始化

 */

private String title, message = "", messagered;

/**

 * 按鈕顯示文本

 */

private String poText, neText, midText;

/**

 * 分割線

 */

private ImageView iv_midle, iv_cancle;

/**

 * 按鈕監聽事件

 */

private View.OnClickListenerpoLis, neLis,midLis;

/**

 * 標題容器

 */

private LinearLayoutlin_title;

/**

 * 彈出框容器

 */

private LinearLayoutline_parent;

/**

 * 調用的Activity實例

 */

private Activity myActivity;

/**

 * 彈出框背景顏色,默認白色

 */

private int dialogBgColor = Color.WHITE;

 

/**

 * 彈出框圓角半徑,默認10,單位dip

 */

private int dialogRadius = 10;

/**

 * 按鈕默認顏色,默認白色

 */

private int btnDefaultColor = Color.WHITE;

/**

 * 按鈕點擊顏色,默認灰色

 */

private int btnPressedColor = Color.GRAY;

/**

 * 標題字體顏色

 */

private int titleColor = Color.BLUE;

/**

 * 標題分割線顏色

 */

private int titleLineColor = Color.BLUE;

 

/**

 * 構造方法,默認設置點擊外部不可消失

 *

 * @param activity

 */

public AAMyAlertDialog(Activityactivity) {

super(activity);

this.myActivity =activity;

this.setCanceledOnTouchOutside(false);

}

 

/**

 * 設置標題

 *

 * @param title

 */

public void setTitle(Stringtitle) {

this.title =title;

}

 

/**

 * 設置顯示內容

 *

 * @param message

 */

public void setMessage(Stringmessage) {

this.message =message;

}

 

/**

 * 設置紅色提醒內容

 *

 * @param message

 */

public void setMessageRed(Stringmessage) {

this.messagered =message;

}

 

/**

 * 設置確定按鈕內容和監聽,當只有一個按鈕時,只設置這個方法,否則佈局會異常

 *

 * @param text

 * @param l

 */

public void setPositiveButton(Stringtext, View.OnClickListener l) {

this.poText =text;

this.poLis =l;

}

 

/**

 * 設置取消按鈕內容和監聽

 *

 * @param text

 * @param l

 */

public void setNegativeButton(Stringtext, View.OnClickListener l) {

this.neText =text;

this.neLis =l;

}

 

/**

 * 設置中間按鈕內容和監聽

 *

 * @param text

 * @param l

 */

public void setMidleButton(Stringtext, View.OnClickListener l) {

this.midText =text;

this.midLis =l;

}

 

/**

 * 設置彈出框背景顏色

 */

public void setDialogBgColor(int color) {

this.dialogBgColor =color;

}

 

/**

 * 設置彈出框圓角半徑

 *

 * @param radius

 */

public void setDialogRadius(int radius) {

this.dialogRadius =radius;

}

 

@Override

public void show() {

super.show();

Window window = getWindow(); // 得到對話框

window.setWindowAnimations(R.style.dialogWindowAnim);// 設置窗口彈出動畫

// 設置對話框背景爲透明

window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

}

 

@Override

public void onAttachedToWindow() {

super.onAttachedToWindow();

Window dialogWindow = getWindow();

WindowManager.LayoutParams lp = dialogWindow.getAttributes();

WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);

 

int width =wm.getDefaultDisplay().getWidth();

// 獲取屏幕寬、高用

// p.height = (int) (d.getHeight() * 0.6); // 高度設置爲屏幕的0.6

lp.width = (int) (width * 0.65);// 寬度設置爲屏幕的0.95

dialogWindow.setAttributes(lp);

}

 

@Override

protected void onCreate(BundlesavedInstanceState) {

super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_NO_TITLE);

// 初始化變量,將半徑的dp轉換爲pix

dialogRadius = dip2px(myActivity,dialogRadius);

setContentView(R.layout.aa_my_alert_dialog);

btn_ok = (Button) findViewById(R.id.btn_ok);

btn_cancle = (Button) findViewById(R.id.btn_cancle);

btn_midle = (Button) findViewById(R.id.btn_midle);

tv_title = (TextView) findViewById(R.id.tv_title);

tv_alert = (TextView) findViewById(R.id.tv_alert);

tv_alertred = (TextView) findViewById(R.id.tv_alertred);

iv_midle = (ImageView) findViewById(R.id.iv_midle);

iv_cancle = (ImageView) findViewById(R.id.iv_cancle);

lin_title = (LinearLayout) findViewById(R.id.lin_title);

line_parent = (LinearLayout) findViewById(R.id.line_parent);

iv_title_line = (ImageView) findViewById(R.id.iv_title_line);

// 初始化佈局顏色

tv_title.setTextColor(titleColor);

iv_title_line.setBackgroundColor(titleLineColor);

// 設置彈出框背景

GradientDrawable mbg = new GradientDrawable();// 創建drawable

mbg.setColor(dialogBgColor);

mbg.setCornerRadius(dialogRadius);

line_parent.setBackgroundDrawable(mbg);

if (message.contains("<html>")) {

tv_alert.setText(Html.fromHtml(message));

} else {

tv_alert.setText(message);

}

// 未設置btn_ok隱藏

btn_ok.setText(poText);

btn_ok.setOnClickListener(poLis);

// 通過按鈕數量設置按鈕樣式

int buttonCount = 1;

// 未設置btn_cancle隱藏

if (neText ==null) {

btn_cancle.setVisibility(8);

iv_cancle.setVisibility(8);

} else {

buttonCount++;

btn_cancle.setVisibility(0);

btn_cancle.setText(neText);

btn_cancle.setOnClickListener(neLis);

iv_cancle.setVisibility(0);

}

if (midText ==null) {

btn_midle.setVisibility(8);

iv_midle.setVisibility(8);

} else {

buttonCount++;

btn_midle.setVisibility(0);

iv_midle.setVisibility(0);

btn_midle.setText(midText);

btn_midle.setOnClickListener(midLis);

}

if (title ==null) {

lin_title.setVisibility(8);

} else {

lin_title.setVisibility(0);

tv_title.setText(title);

}

if (messagered ==null) {

tv_alertred.setVisibility(8);

} else {

tv_alertred.setText(messagered);

}

// ------------------設置按鈕點擊效果

if (buttonCount == 1) {

// 默認背景

GradientDrawable gd1 = new GradientDrawable();// 創建drawable

gd1.setColor(btnDefaultColor);

gd1.setCornerRadii(new float[] { 0, 0, 0, 0,dialogRadius, dialogRadius, dialogRadius, dialogRadius });

// 點擊背景

GradientDrawable gd2 = new GradientDrawable();// 創建drawable

gd2.setColor(btnPressedColor);

gd2.setCornerRadii(new float[] { 0, 0, 0, 0,dialogRadius, dialogRadius, dialogRadius, dialogRadius });

// 定義選擇器

StateListDrawable selector = new StateListDrawable();

selector.addState(new int[] { -android.R.attr.state_pressed },gd1);

selector.addState(new int[] { android.R.attr.state_pressed },gd2);

btn_ok.setBackgroundDrawable(selector);

}

if (buttonCount == 2) {

try {

// 默認背景

GradientDrawable gd1 = new GradientDrawable();// 創建drawable

gd1.setColor(btnDefaultColor);

gd1.setCornerRadii(new float[] { 0, 0, 0, 0,dialogRadius, dialogRadius, 0, 0 });

// 點擊背景

GradientDrawable gd2 = new GradientDrawable();// 創建drawable

gd2.setColor(btnPressedColor);

gd2.setCornerRadii(new float[] { 0, 0, 0, 0,dialogRadius, dialogRadius, 0, 0 });

// 定義選擇器

StateListDrawable selector = new StateListDrawable();

selector.addState(new int[] { -android.R.attr.state_pressed },gd1);

selector.addState(new int[] { android.R.attr.state_pressed },gd2);

btn_cancle.setBackgroundDrawable(selector);

} catch (Exception e) {

e.printStackTrace();

}

 

try {

// 默認背景

GradientDrawable gd1 = new GradientDrawable();// 創建drawable

gd1.setColor(btnDefaultColor);

gd1.setCornerRadii(new float[] { 0, 0, 0, 0, 0, 0,dialogRadius, dialogRadius });

// 點擊背景

GradientDrawable gd2 = new GradientDrawable();// 創建drawable

gd2.setColor(btnPressedColor);

gd2.setCornerRadii(new float[] { 0, 0, 0, 0, 0, 0,dialogRadius, dialogRadius });

// 定義選擇器

StateListDrawable selector = new StateListDrawable();

selector.addState(new int[] { -android.R.attr.state_pressed },gd1);

selector.addState(new int[] { android.R.attr.state_pressed },gd2);

btn_ok.setBackgroundDrawable(selector);

} catch (Exception e) {

e.printStackTrace();

}

}

if (buttonCount == 3) {

 

try {

// 默認背景

GradientDrawable gd1 = new GradientDrawable();// 創建drawable

gd1.setColor(btnDefaultColor);

gd1.setCornerRadii(new float[] { 0, 0, 0, 0,dialogRadius, dialogRadius, 0, 0 });

// 點擊背景

GradientDrawable gd2 = new GradientDrawable();// 創建drawable

gd2.setColor(btnPressedColor);

gd2.setCornerRadii(new float[] { 0, 0, 0, 0,dialogRadius, dialogRadius, 0, 0 });

// 定義選擇器

StateListDrawable selector = new StateListDrawable();

selector.addState(new int[] { -android.R.attr.state_pressed },gd1);

selector.addState(new int[] { android.R.attr.state_pressed },gd2);

btn_cancle.setBackgroundDrawable(selector);

} catch (Exception e) {

e.printStackTrace();

}

 

try {

// 默認背景

GradientDrawable gd1 = new GradientDrawable();// 創建drawable

gd1.setColor(btnDefaultColor);

gd1.setCornerRadii(new float[] { 0, 0, 0, 0, 0, 0,dialogRadius, dialogRadius });

// 點擊背景

GradientDrawable gd2 = new GradientDrawable();// 創建drawable

gd2.setColor(btnPressedColor);

gd2.setCornerRadii(new float[] { 0, 0, 0, 0, 0, 0,dialogRadius, dialogRadius });

// 定義選擇器

StateListDrawable selector = new StateListDrawable();

selector.addState(new int[] { -android.R.attr.state_pressed },gd1);

selector.addState(new int[] { android.R.attr.state_pressed },gd2);

btn_ok.setBackgroundDrawable(selector);

} catch (Exception e) {

e.printStackTrace();

}

 

try {

// 默認背景

GradientDrawable gd1 = new GradientDrawable();// 創建drawable

gd1.setColor(btnDefaultColor);

// 點擊背景

GradientDrawable gd2 = new GradientDrawable();// 創建drawable

gd2.setColor(btnPressedColor);

// 定義選擇器

StateListDrawable selector = new StateListDrawable();

selector.addState(new int[] { -android.R.attr.state_pressed },gd1);

selector.addState(new int[] { android.R.attr.state_pressed },gd2);

btn_midle.setBackgroundDrawable(selector);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

 

/**

 * 根據手機的分辨率從 dp 的單位 轉成爲px(像素)

 */

private int dip2px(Contextcontext, float dpValue) {

final float scale =context.getResources().getDisplayMetrics().density;

return (int) (dpValue *scale + 0.5f);

}

 

/**

 * 根據手機的分辨率從 px(像素) 的單位 轉成爲dp

 */

private int px2dip(Contextcontext, float pxValue) {

final float scale =context.getResources().getDisplayMetrics().density;

return (int) (pxValue /scale + 0.5f);

}

}

aa_common_animations.xml文件

<?xml version="1.0" encoding="utf-8"?>

<resources>

 

    <style name="dialogWindowAnim" mce_bogus="1" parent="android:Animation">

        <item name="android:windowEnterAnimation">@anim/aa_dailog_enter_animation</item>

        <item name="android:windowExitAnimation">@anim/aa_dailog_exit_animation</item>

    </style>

 

    <color name="black">#323232</color>

 

</resources>

aa_my_alert_dialog.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:id="@+id/line_parent"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:orientation="vertical" >

 

    <LinearLayout

        android:id="@+id/lin_title"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:gravity="center_vertical"

        android:orientation="vertical" >

 

        <TextView

            android:id="@+id/tv_title"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_margin="10dp"

            android:text="標題"

            android:textSize="18dp" />

 

        <ImageView

            android:id="@+id/iv_title_line"

            android:layout_width="fill_parent"

            android:layout_height="1dp" />

    </LinearLayout>

 

    <LinearLayout

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:orientation="vertical"

        android:padding="10dip" >

 

        <TextView

            android:id="@+id/tv_alert"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="內容"

            android:textColor="@color/black"

            android:textSize="18sp" />

 

        <TextView

            android:id="@+id/tv_alertred"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:gravity="center_vertical"

            android:textColor="#ffff4444"

            android:textSize="18sp"

            android:textStyle="bold" />

    </LinearLayout>

 

    <ImageView

        android:layout_width="fill_parent"

        android:layout_height="1dp"

        android:layout_marginTop="5dip"

        android:background="#999999" />

 

    <LinearLayout

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:orientation="horizontal" >

 

        <Button

            android:id="@+id/btn_ok"

            android:layout_width="0dp"

            android:layout_height="38dp"

            android:layout_weight="1"

            android:gravity="center"

            android:text="確 定 "

            android:textColor="@color/black"

            android:textSize="16sp" />

 

        <ImageView

            android:id="@+id/iv_midle"

            android:layout_width="1dp"

            android:layout_height="fill_parent"

            android:background="#999999" />

 

        <Button

            android:id="@+id/btn_midle"

            android:layout_width="0dp"

            android:layout_height="38dp"

            android:layout_weight="1"

            android:gravity="center"

            android:text="返回主菜單"

            android:textColor="@color/black"

            android:textSize="16sp" />

 

        <ImageView

            android:id="@+id/iv_cancle"

            android:layout_width="1dp"

            android:layout_height="fill_parent"

            android:background="#999999" />

 

        <Button

            android:id="@+id/btn_cancle"

            android:layout_width="0dp"

            android:layout_height="38dp"

            android:layout_weight="1"

            android:gravity="center"

            android:text="取 消"

            android:textColor="@color/black"

            android:textSize="16sp" />

    </LinearLayout>

 

</LinearLayout>

aa_dailog_enter_animation.xml文件

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android"

    android:interpolator="@android:anim/linear_interpolator">

 

    <scale

        android:duration="200"

        android:fromXScale="0"

        android:fromYScale="0"

        android:pivotX="50%"

        android:pivotY="50%"

        android:toXScale="1"

        android:toYScale="1"/>

 

    <alpha

        android:duration="200"

        android:fromAlpha="0"

        android:toAlpha="1"/>

 

</set>

aa_dailog_exit_animation.xml文件

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android"

    android:interpolator="@android:anim/linear_interpolator">

 

    <scale

        android:duration="200"

        android:fromXScale="1"

        android:fromYScale="1"

        android:pivotX="50%"

        android:pivotY="50%"

        android:toXScale="0"

        android:toYScale="0"/>

    <alpha

        android:duration="200"

        android:fromAlpha="1"

        android:toAlpha="0"/>

</set>


源碼下載地址:http://download.csdn.net/detail/haiyangzhisheng/9504180

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章