Android进阶篇-自定义Dialog(一)

DialogCommon.java:

package com.easier.gallery.common;

import com.easier.gallery.R;

import android.R.integer;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class DialogCommon extends AlertDialog{
    private int id;
    private String title;//标题
    private View contentView;//dialog的view
    private String message;//dialog显示内容
    private String leftButton;//左边按钮
    private String rightButton;//右边按钮
    private int tag;//dialog标示
    private Button leftBtn;//左边按钮
    private Button rightBtn;//右边按钮
    private TextView titleTv;//标题
    private TextView CountMessage;//信息内容
    private View dialogLine;//分割线
    private EditText mEditText;//输入框

    private Context context;
    private DialogBtnInterface dialogBtn;//按钮接口

    private final static int TYPE_DEFAULT = -1;
    private final static int TYPE_EDITTEXT = 0;
    private int TYPE = TYPE_DEFAULT;

    protected DialogCommon(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }
    public DialogCommon(Context context,DialogBtnInterface dialogBtn, String title,String leftButton,String rightButton){
        super(context);
        this.context = context;
        this.title = title;
        this.dialogBtn = dialogBtn;
        this.leftButton = leftButton;
        this.rightButton = rightButton;
        tag = 0;
    }
    public DialogCommon(Context context,DialogBtnInterface dialogBtn, String title,View contentView,String leftButton,String rightButton){
        this(context,dialogBtn,title,leftButton,rightButton);
        this.contentView = contentView;
        tag = 1;
    }
    public DialogCommon(Context context,DialogBtnInterface dialogBtn, String title,String message,String leftButton,String rightButton){
        this(context,dialogBtn,title,leftButton,rightButton);
        this.message = message;
        tag = 2;
    }
    public DialogCommon(Context context,DialogBtnInterface dialogBtn,String message,String rightButton){
        super(context);
        this.context = context;
        this.dialogBtn = dialogBtn;
        this.message = message;
        this.rightButton = rightButton;
        tag = 3;
    }
    
    public void setEditItem(){
        mEditText = new EditText(getContext());
        setView(mEditText);
        TYPE = TYPE_EDITTEXT;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        
//        this.setContentView(R.layout.dialog_message);
//        leftBtn = (Button) this.findViewById(R.id.dialog_left_btn);
//        rightBtn = (Button) this.findViewById(R.id.dialog_right_btn);
//        titleTv = (TextView) this.findViewById(R.id.dialog_title);
//        CountMessage = (TextView) this.findViewById(R.id.dialogMessage);
//        CountMessage.setWidth((int)StaticData.W*11/12);
//        dialogLine = (View) this.findViewById(R.id.dialog_line);
        if (tag == 0){
            this.setIcon(0);
            this.setTitle(title);
        }else if (tag == 1) {
            this.setIcon(0);
            this.setTitle(title);
            this.setView(contentView);
        } else if (tag == 2) {
//            
//            titleTv.setVisibility(View.VISIBLE);
//            titleTv.setText(title);
//            CountMessage.setText(message);
            
            this.setIcon(0);
            this.setTitle(title);
            View flowCountView = LayoutInflater.from(context).inflate(
                    R.layout.dialog_message, null);
            TextView flowCountMessage = (TextView) flowCountView
                    .findViewById(R.id.dialogMessage);
            flowCountMessage.setText(message);
            this.setView(flowCountView, 10, 20, 10, 20);
        } else if (tag == 3) {
            Typeface typeface = Typeface.create("黑体", Typeface.NORMAL);
//            CountMessage.setTypeface(typeface);
//            CountMessage.setText(message);
            View flowCountView = LayoutInflater.from(context).inflate(
                    R.layout.dialog_message, null);
            TextView flowCountMessage = (TextView) flowCountView
                    .findViewById(R.id.dialogMessage);
            flowCountMessage.setTypeface(typeface);
            flowCountMessage.setText(message);
            this.setView(flowCountView, 15, 15, 15, 15);
        }

        if (tag != 3) {
//            dialogLine.setVisibility(View.VISIBLE);
//            leftBtn.setVisibility(View.VISIBLE);
//            leftBtn.setText(leftButton);
//            leftBtn.setOnClickListener(new View.OnClickListener() {
//                
//                @Override
//                public void onClick(View v) {
//                    // TODO Auto-generated method stub
//                    dialogBtn.okBtn(DialogCommon.this);
//                }
//            });
            this.setButton(leftButton, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        if(TYPE == TYPE_EDITTEXT){
                            dialogBtn.confirmText(DialogCommon.this, mEditText.getText().toString());
                        } else if (TYPE == TYPE_DEFAULT){
                            dialogBtn.okBtn(DialogCommon.this);
                        }
                    }
                });
        }
//        rightBtn.setText(rightButton);
//        rightBtn.setOnClickListener(new View.OnClickListener() {
//            
//            @Override
//            public void onClick(View v) {
//                // TODO Auto-generated method stub
//                dialogBtn.cancelBtn(DialogCommon.this);
//            }
//        });
        this.setButton2(rightButton, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                dialogBtn.cancelBtn(DialogCommon.this);
            }
            });
        
        super.onCreate(savedInstanceState);
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }


    public DialogBtnInterface getDialogBtn() {
        return dialogBtn;
    }


    public void setDialogBtn(DialogBtnInterface dialogBtn) {
        this.dialogBtn = dialogBtn;
    }
    
}

DialogBtnInterface.java:

public interface DialogBtnInterface {

     /**
     * 点击alertDialog的取消按钮
     * 
     * @param dialog 当前dialog 
     * */
    public abstract void cancelBtn(AlertDialog dialog);
    
    /**
     * 点击alertDialog的确认按钮,子类需重载此方法
     * 
     * @param dialog 当前dialog 
     * */
    public abstract void okBtn(AlertDialog dialog);
    
    public abstract void confirmText(AlertDialog dialog, String text);
}

转载于:https://www.cnblogs.com/gongcb/archive/2012/05/10/2494563.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值