• 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);
    }
  • 相关阅读:
    Xpath提取一个标签里的所有文本
    Mac Sublime Text 3
    激活pycharm
    Mac 终端命令大全
    操作dict时避免出现KeyError的几种方法
    JQ 选择器大全
    调用百度编辑器
    模拟 Ajax 上传
    图片上传
    PHP 字符串去掉空格
  • 原文地址:https://www.cnblogs.com/gongcb/p/2494563.html
Copyright © 2020-2023  润新知