先看效果
看到这个就想到系统提供的Dialog,但是各个版本的又不一样,有的很难看,so…继承他们,自定义一个
先写一下自己需要的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="280dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:orientation="vertical"
android:paddingLeft="10dp">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:padding="5dp"
android:text="@string/prompt"
android:textColor="#505050"
android:textSize="18dp"
android:visibility="visible" />
<TextView
android:id="@+id/alert_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"
android:gravity="center_horizontal"
android:padding="5dp"
android:textColor="#505050"
android:textSize="16dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:gravity="right"
android:orientation="horizontal"
android:paddingBottom="8dp">
<Button
android:id="@+id/btn_cancel"
style="?android:attr/borderlessButtonStyle"
android:layout_width="65dp"
android:layout_height="32dp"
android:background="@drawable/ease_btn_cancel_bj"
android:gravity="center"
android:text="@string/cancel"
android:textColor="#969696"
android:textSize="15dp" />
<Button
android:id="@+id/btn_ok"
style="?android:attr/borderlessButtonStyle"
android:layout_width="65dp"
android:layout_height="32dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp"
android:background="@drawable/ease_btn_cancel_bj"
android:gravity="center"
android:text="@string/ok"
android:textColor="#09aaf5"
android:textSize="15dp" />
</LinearLayout>
</LinearLayout>
效果就是这样的
然后写逻辑代码,新起一个类,继承自Dialog
class EaseAlertDialog extends Dialog
声明需要的字段
private String title;//标题
private String msg;//提示消息
private CallBack callBack;
private boolean showCancel = false;//是否显示取消按钮
private Bundle bundle;//扩展字段
定义一个按钮点击回调接口
public interface CallBack {
void onResult(boolean confirmed, Bundle bundle);
}
编写构造方法(可以写多个,通过不同的参数来控制不同显示方式)
//通用的构造方法
public EaseAlertDialog(Context context, String title, String msg, Bundle bundle, CallBack callBack, boolean showCancel) {
super(context);
this.title = title;
this.msg = msg;
this.callBack = callBack;
this.bundle = bundle;
this.showCancel = showCancel;
this.setCanceledOnTouchOutside(true);//点击外部是否可关闭
}
//只显示一个取消按钮,不可点击外部关闭
public EaseAlertDialog(Context context, String msg, AlertDialogUser user, boolean showCancel) {
super(context);
this.msg = msg;
this.user = user;
this.showCancel = showCancel;
this.setCanceledOnTouchOutside(false);//设置点击外部不可关闭
}
重写onCreate方法,绑定布局,初始化控件,设置按钮点击事件
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.ease_alert_dialog);//绑定布局
//声明控件
Button cancel = (Button) findViewById(R.id.btn_cancel);
Button ok = (Button) findViewById(R.id.btn_ok);
TextView titleView = (TextView) findViewById(R.id.title);
TextView msgView = (TextView) findViewById(R.id.alert_message);
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View view) {
if (view.getId() == R.id.btn_ok) {
onOk(view);
} else if (view.getId() == R.id.btn_cancel) {
onCancel(view);
}
}
};
//设置按钮点击事件
cancel.setOnClickListener(listener);
ok.setOnClickListener(listener);
//设置标题
if (title != null) {
titleView.setText(title);
} else {
titleView.setVisibility(View.GONE);
}
//设置提示内容
if (msg != null){
msgView.setText(msg);
}else{
msgView.setVisibity(View.GONE);
}
//是否显示取消按钮
if (showCancel) {
cancel.setVisibility(View.VISIBLE);
} else {
cancel.setVisibility(View.GONE);
}
}
public void onOk(View view) {
this.dismiss();
if (this.user != null) {
this.user.onResult(true, this.bundle);
}
}
public void onCancel(View view) {
this.dismiss();
if (this.user != null) {
this.user.onResult(false, this.bundle);
}
}
好了,结束了,等会附上源码下载链接