2015年8月21日 星期五

Android-Dialog 範例 說明

更新日期 : 20160130 更新代碼錯誤

為甚麼要用 Dialog?
因為方便提醒使用者現在程式的狀況
例如 :
登入遊戲卻遲遲無反應。
結果是帳號密碼輸入錯誤
如果不給 User 提示
那因該不管是誰都會很囧吧

官方已經有範例
但是要更有彈性就只能自己做更改

import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
/**
 * BaseDialog Tool
 * The DialogInterface.OnClickListener is default
 * @Override
    public void onClick(DialogInterface dialog, int which) {
        switch(which)
        {
        case AlertDialog.BUTTON_POSITIVE:
            break;
        case AlertDialog.BUTTON_NEGATIVE:
            break;
        case AlertDialog.BUTTON_NEUTRAL:
            break;
        }
    }
 *
 */
public class MyDialog{

    private Context context;
    private String title;
    private String message;
    private String positiveText;
    private String negativeText;
    private String neutralText;
    private AlertDialog.Builder builder;
    private DialogInterface.OnClickListener positiveDialogCallBack;
    private DialogInterface.OnClickListener negativeDialogCallBack;
    private DialogInterface.OnClickListener neutralDialogCallBack;

    public MyDialog(Context context){
        this.context = context;
    }

    /** set dialog title text */
    public MyDialog setTitle(String title){
        this.title = title;    
        return this;
    }

    /** set dialog title text */
    public MyDialog setTitle(int r){
        this.title = context.getResources().getString(r);
        return this;
    }

    /** set dialog message text */
    public MyDialog setMessage(String message){
        this.message = message;    
        return this;
    }

    /** set dialog message text */
    public MyDialog setMessage(int r){
        this.message = context.getResources().getString(r);    
        return this;
    }

    /** set positive button text */
    public MyDialog setPositiveText(String positiveText){
        this.positiveText = positiveText;
        return this;
    }

    /** set positive button text */
    public MyDialog setPositiveText(int r){
        this.positiveText = context.getResources().getString(r);;
        return this;
    }

    /** set negative button text */
    public MyDialog setNegativeText(String negativeText){
        this.negativeText = negativeText;
        return this;
    }

    /** set negative button text */
    public MyDialog setNegativeText(int r){
        this.negativeText = context.getResources().getString(r);;
        return this;
    }
    
    /** set neutral button text */
    public MyDialog setNeutralText(String neutralText){
        this.neutralText = neutralText;
        return this;
    }

    /** set neutral button text */
    public MyDialog setNeutralText(int r){
        this.neutralText = context.getResources().getString(r);;
        return this;
    }

    /** AlertDialog.BUTTON_POSITIVE */
    public MyDialog setOnPositiveListener(
    DialogInterface.OnClickListener onPositiveListener) {
    
        this.positiveDialogCallBack = onPositiveListener;
        return this;
    }

    /** AlertDialog.BUTTON_NEGATIVE */
    public MyDialog setOnNegativeListener(
        DialogInterface.OnClickListener onNegativeListener) {
        
        this.negativeDialogCallBack = onNegativeListener;
        return this;
    }
    
    /** AlertDialog.BUTTON_NEGATIVE */
    public MyDialog setOnNeutralListenerListener(
        DialogInterface.OnClickListener onNeutralListener) {
        
        this.neutralDialogCallBack = onNeutralListener;
        return this;
    }
    
    /** Display you setting dialog */
    public void show() {
        builder = new AlertDialog.Builder(context);
        if(title != null)
            builder.setTitle(title);
        if(message != null)
            builder.setMessage(message);
        if(positiveText != null || positiveDialogCallBack != null)
            builder.setPositiveButton(positiveText, positiveDialogCallBack);
        if(negativeText != null || negativeDialogCallBack != null)
            builder.setNegativeButton(negativeText, negativeDialogCallBack);
        if(neutralText != null || neutralDialogCallBack != null)
            builder.setNeutralButton(neutralText, neutralDialogCallBack);
        builder.create().show();
    }
}

new MyDialog(MainActivity.this).setTitle("Hello").setMessage("How are you?")
                .setNeutralText("Neutral").setNegativeText("Negative").setPositiveText("Positive")
                .show();














參考資訊 : How to handle Back button with in the dialog?
參考資訊 : AlertDialog
參考資料 : AlertDialog_zh

歡迎轉載,請註明出處。

沒有留言:

張貼留言