美文网首页
Android 自定义Dialog

Android 自定义Dialog

作者: 用心感受世界 | 来源:发表于2017-05-23 15:34 被阅读0次

    自定义按钮的实现,很多自定义的控件都可以以类似的方式实现。比较简单

     public class SampleDialog extends Dialog {
        @BindView(R.id.content)
        TextView contextTxt;
        @BindView(R.id.title)
        TextView titleTxt;
        @BindView(R.id.submit)
        TextView submitTxt;
        @BindView(R.id.cancel)
        TextView cancelTxt;
    
        private Context mContext;
        private String content;//提示内容
        private OnCloseListener listener;//按钮监听
        private String positiveName;//确认文本
        private String negativeName;//取消文本
        private String title;//标题
    
        //Dialog本身的构造函数就不添加了,实现的时候需补上,这里放上需要的构造函数
        public SampleDialog(Context context, int themeResId, String content, OnCloseListener listener) {
            super(context, themeResId);
            this.mContext = context;
            this.content = content;
            this.listener = listener;
        }
    
        //设置标题
        public SampleDialog setTitle(String title){
            this.title = title;
            return this;
        }
    
       //设置确认文本
        public SampleDialog setPositiveButton(String name){
            this.positiveName = name;
            return this;
        }
    
     //设置取消文本
        public SampleDialog setNegativeButton(String name){
            this.negativeName = name;
            return this;
        }
    
         //设置确认点击事件
        @OnClick(R.id.submit)
        void clickSubmit(){
            if(listener != null){
                listener.onClick(this, true);
            }
            this.dismiss();
        }
    
      //设置确认取消点击事件
        @OnClick(R.id.cancel)
        void clickCancel(){
            if(listener != null){
                listener.onClick(this,false);
            }
            this.dismiss();
        }
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.dialog_commom);//设置dialog,具体的样式请随意发挥,只需保证有4个控件,标题,内容文本,确认按钮,取消按钮
            setCanceledOnTouchOutside(false);
            ButterKnife.bind(this);
            initView();
        }
    
        //初始化文本
        private void initView(){
            contextTxt.setText(content);
            if(!TextUtils.isEmpty(positiveName)){
                submitTxt.setText(positiveName);
            }
    
            if(!TextUtils.isEmpty(negativeName)){
                cancelTxt.setText(negativeName);
            }
    
            if(!TextUtils.isEmpty(title)){
                titleTxt.setText(title);
            }
    
        }
    
      //接口,需在实际使用中实现
        public interface OnCloseListener{
            void onClick(Dialog dialog, boolean confirm);
        }
    }
    

    实现的例子

    SampleDialog dialog2 = new SampleDialog(xxxActivity.this,R.style.dialog, "hello world", new SampleDialog.OnCloseListener() {
                        @Override
                        public void onClick(Dialog dialog, boolean confirm) {
                            if(confirm){
                                //DO YES
                            }else{
                                //DO NO
                                dialog.dismiss();//关闭dialog
                            }
                        }
                    });
                    dialog2.setPositiveButton("是!");
                    dialog2.setNegativeButton("我点错啦!");
                    dialog2.show();
    

    补充两个内容,dialog的THEME,以及 animation 效果
    values/styles.xml

    <style name="dialog" parent="@android:style/Theme.Dialog">
            <item name="android:windowFrame">@null</item>
            <!--边框-->
            <item name="android:windowIsFloating">true</item>
            <!--是否浮现在activity之上-->
            <item name="android:windowIsTranslucent">false</item>
            <!--半透明-->
            <item name="android:windowNoTitle">true</item>
            <!--无标题-->
            <item name="android:windowBackground">@android:color/transparent</item>
            <!--背景透明-->
            <item name="android:backgroundDimEnabled">true</item>
            <!--模糊-->
    
        </style>
    
    
        <style name="dialogWindowAnim" mce_bogus="1" parent="android:Animation">
            <item name="android:windowEnterAnimation">@anim/dialog_enter_anim</item>
            <item name="android:windowExitAnimation">@anim/dialog_exit_anim</item>
        </style>
    

    动画
    动画:1 anim/dialog_enter_anim

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
        <translate
            android:duration="200"
            android:fromYDelta="100%p"
            android:toYDelta="0" />
        <alpha
            android:duration="200"
            android:fromAlpha="0.0"
            android:toAlpha="1.0" />
    </set>
    

    动画:2 anim/dialog_exit_anim

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
        <translate
            android:duration="200"
            android:fromYDelta="0"
            android:toYDelta="50%p" />
        <alpha
            android:duration="200"
            android:fromAlpha="1.0"
            android:toAlpha="0.0" />
    </set>
    

    相关文章

      网友评论

          本文标题:Android 自定义Dialog

          本文链接:https://www.haomeiwen.com/subject/ntfgxxtx.html