androidstudio自定义Dialog

androidstudio自定义Dialog_第1张图片

新建一个XML文件,文件名为layout_custom_dialog

<?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:gravity="center_horizontal"
    android:orientation="vertical"
    android:background="@drawable/bg_custom_dialog">

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="提示"
        android:textSize="20sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/tv_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="删除?"
        android:textSize="20sp"
        android:textStyle="bold" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/black"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tv_no"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="取消"
            android:textSize="20sp"
            android:textStyle="bold"
            android:gravity="center"/>

        <View
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="@color/black"/>

        <TextView
            android:id="@+id/tv_ok"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="确定"
            android:textSize="20sp"
            android:textStyle="bold"
            android:gravity="center"/>



    </LinearLayout>
    >


</LinearLayout>

在drawable文件下新建XML文件,文件名为bg_custom_dialog,定义了圆角

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="10dp"/>
    <solid android:color="@color/white"/>
</shape>

新建CustomDialog的类

public class CustomDialog extends Dialog implements View.OnClickListener {
    private TextView mTitle,mTvMessage,mConfirm,mCancel;

    private String title,message,confirm,cancel;

    private OnCancellistener cancellistener;
    private OnConfirmlistener confirmlistener;

    //先写构造方法
    public CustomDialog(@NonNull Context context) {
        super(context);
    }

    public CustomDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void setConfirm(String confirm,OnConfirmlistener listener) {
        this.confirm = confirm;
        this.confirmlistener=listener;
    }

    public void setCancel(String cancel,OnCancellistener listener) {
        this.cancel = cancel;
        this.cancellistener=listener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_custom_dialog);
        //设置宽度
        WindowManager manager= getWindow().getWindowManager();
        Display display=manager.getDefaultDisplay();
        WindowManager.LayoutParams params=getWindow().getAttributes();
        Point size=new Point();
        display.getSize(size);
        params.width=(int)(size.x*0.8);
        getWindow().setAttributes(params);
        //实现圆角
        getWindow().setBackgroundDrawableResource(R.drawable.bg_custom_dialog);

        mTitle=(TextView) findViewById(R.id.tv_title);
        mTvMessage=(TextView) findViewById(R.id.tv_message);
        mConfirm=(TextView) findViewById(R.id.tv_ok);
        mCancel=(TextView) findViewById(R.id.tv_no);

        if(TextUtils.isEmpty(title)){
            mTitle.setText(title);
        }
        if(TextUtils.isEmpty(message)){
            mTvMessage.setText(message);
        }
        if(TextUtils.isEmpty(title)){
            mConfirm.setText(confirm);
        }
        if(TextUtils.isEmpty(title)){
            mCancel.setText(cancel);
        }
        mConfirm.setOnClickListener(this);
        mCancel.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tv_no:
                if(cancellistener!=null){
                cancellistener.onCancel(this);
            }
                dismiss();
                break;
            case R.id.tv_ok:
                if(confirmlistener!=null){
                    confirmlistener.onConfirm(this);
                }
                dismiss();
                break;
        }
    }

    public interface OnConfirmlistener{
        void onConfirm(CustomDialog dialog);
    }
    public interface OnCancellistener{
        void onCancel(CustomDialog dialog);
    }
}

在MainActivity里实现CustomDialog这个类

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button mBtn_Dialog=(Button) findViewById(R.id.dialog);
        mBtn_Dialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CustomDialog customDialog=new CustomDialog(MainActivity.this);
                customDialog.setTitle("提示");
                customDialog.setMessage("删除?");
                customDialog.setConfirm("确认", new CustomDialog.OnConfirmlistener() {
                    @Override
                    public void onConfirm(CustomDialog dialog) {

                    }
                });
                customDialog.setCancel("取消", new CustomDialog.OnCancellistener() {
                    @Override
                    public void onCancel(CustomDialog dialog) {

                    }
                });
                customDialog.show();
            }
        });
    }
}

你可能感兴趣的:(android,java,开发语言)