package com.example.builderdemo;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void showDialog(View view) {
new MyDialog.Builder(this)
.setTitle("我是dialog的标题 ")
.setMessage("我是消息内容")
.setConfirmButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "我点击了确定按钮", Toast.LENGTH_SHORT).show();
}
})
.setCancelButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "我点击了取消按钮", Toast.LENGTH_SHORT).show();
}
})
.create().show();
}
}
package com.example.builderdemo;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.TextView;
public class MyDialog extends Dialog {
public MyDialog(@NonNull Context context) {
super(context);
}
public MyDialog(@NonNull Context context, int themeResId) {
super(context, themeResId);
}
protected MyDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
}
public static class Builder{
private Context context;
private String title="提示";
private String message="是否确定";
private String confirmText="确定";
private String cancelText="取消";
private DialogInterface.OnClickListener confirmListener;//确认点击事件
private DialogInterface.OnClickListener cancelListener;//取消点击事件
private View dialogView;//对话框视图
public Builder(Context context){
this.context = context;
}
public String getTitle() {
return title;
}
public Builder setTitle(String title) {
this.title = title;
return this;
}
public String getMessage() {
return message;
}
public Builder setMessage(String message) {
this.message = message;
return this;
}
//设置确定按钮
public Builder setConfirmButton(String confirmText, DialogInterface.OnClickListener listener){
this.confirmText = confirmText;
this.confirmListener = listener;
return this;
}
//设置取消按钮
public Builder setCancelButton(String cancelText, DialogInterface.OnClickListener listener){
this.cancelText = cancelText;
this.cancelListener = listener;
return this;
}
//构建Dialog
public MyDialog create(){
dialogView = View.inflate(context, R.layout.dialog_view, null);
final MyDialog myDialog = new MyDialog(context,R.style.myDialog);
myDialog.setContentView(dialogView, new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
TextView tv_title = (TextView) dialogView.findViewById(R.id.tv_title);
tv_title.setText(getTitle());
TextView tv_message = (TextView) dialogView.findViewById(R.id.tv_message);
tv_message.setText(getMessage());
Button btn_confirm = (Button) dialogView.findViewById(R.id.btn_confirm);
btn_confirm.setText(confirmText);
if(confirmListener != null){
btn_confirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
confirmListener.onClick(myDialog, DialogInterface.BUTTON_POSITIVE);
myDialog.dismiss();
}
});
}else{
btn_confirm.setVisibility(View.GONE);
}
Button btn_cancel = (Button) dialogView.findViewById(R.id.btn_cancel);
btn_cancel.setText(cancelText);
if(cancelListener != null){
btn_cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cancelListener.onClick(myDialog, DialogInterface.BUTTON_NEGATIVE);
myDialog.dismiss();
}
});
}else{
btn_cancel.setVisibility(View.GONE);
}
return myDialog;
}
}
}
网友评论