常见Dialog

来源:互联网 发布:vue.js 动态添加class 编辑:程序博客网 时间:2024/05/05 05:56
public class MyDialog extends Dialog implements View.OnClickListener{    private TextView contentTxt;    private TextView titleTxt;    private TextView submitTxt;    private TextView cancelTxt;    private Context mContext;    private String content;    private OnCloseListener listener;    private String positiveName;    private String negativeName;    private String title;    public MyDialog(Context context) {        super(context);        this.mContext = context;    }    public MyDialog(Context context, String content) {        super(context, R.style.dialog);        this.mContext = context;        this.content = content;    }    public MyDialog(Context context, int themeResId, String content) {        super(context, themeResId);        this.mContext = context;        this.content = content;    }    public MyDialog(Context context, int themeResId, String title,String content, OnCloseListener listener) {        super(context, themeResId);        this.mContext = context;        this.content = content;        this.listener = listener;        this.title=title;    }    protected MyDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {        super(context, cancelable, cancelListener);        this.mContext = context;    }    public MyDialog setTitle(String title){        this.title = title;        return this;    }    public MyDialog setPositiveButton(String name){        this.positiveName = name;        return this;    }    public MyDialog setNegativeButton(String name){        this.negativeName = name;        return this;    }    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.view_dialog_common);        setCanceledOnTouchOutside(false);        initView();    }    private void initView(){        contentTxt = (TextView)findViewById(R.id.content);        titleTxt = (TextView)findViewById(R.id.title);        submitTxt = (TextView)findViewById(R.id.submit);        submitTxt.setOnClickListener(this);        cancelTxt = (TextView)findViewById(R.id.cancel);        cancelTxt.setOnClickListener(this);        contentTxt.setText(content);        if(!TextUtils.isEmpty(positiveName)){            submitTxt.setText(positiveName);        }        if(!TextUtils.isEmpty(negativeName)){            cancelTxt.setText(negativeName);        }        if(!TextUtils.isEmpty(title)){            titleTxt.setText(title);        }    }    @Override    public void onClick(View v) {        switch (v.getId()){            case R.id.cancel:                if(listener != null){                    listener.onClick(this, false);                }                this.dismiss();                break;            case R.id.submit:                if(listener != null){                    listener.onClick(this, true);                }                break;        }    }    public interface OnCloseListener{        void onClick(Dialog dialog, boolean confirm);    }}
style
<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>
布局文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@drawable/bg_round_white"    android:orientation="vertical" >        <TextView            android:id="@+id/title"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:gravity="center_horizontal"            android:padding="12dp"            android:layout_marginTop="12dp"            android:text="提示"            android:textSize="16sp"            android:textColor="@color/black"/>    <TextView        android:id="@+id/content"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center"        android:layout_gravity="center_horizontal"        android:lineSpacingExtra="3dp"        android:layout_marginLeft="40dp"        android:layout_marginTop="20dp"        android:layout_marginRight="40dp"        android:layout_marginBottom="30dp"        android:text=""        android:textSize="12sp"        android:textColor="@color/main_text_color"/>    <View        style="@style/line_thin_full"/>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="50dp"        android:orientation="horizontal">        <TextView            android:id="@+id/cancel"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:layout_weight="1.0"            android:gravity="center"            android:text="取消"            android:textSize="12sp"            android:textColor="@color/main_text_color"/>        <View            android:layout_width="1dp"            android:layout_height="match_parent"            android:background="@color/divider_color"/>        <TextView            android:id="@+id/submit"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:gravity="center"            android:layout_weight="1.0"            android:text="确定"            android:textSize="12sp"            android:textColor="@color/common_color1"/>    </LinearLayout></LinearLayout>
用法
new MyDialog(mContext, R.style.dialog, "温馨提示", msg, new MyDialog.OnCloseListener() {    @Override    public void onClick(Dialog dialog, boolean confirm) {        if (confirm) {            checkIfDelete(position, bankList.get(position).getBankCard(), bankList.get(position).getType(), dialog);        } else {            dialog.dismiss();        }    }}).show();


原创粉丝点击