在开发中需要弹出dialog,有的需要有不在提醒的功能,这样的系统没有,只能自己自定义,下面就看一下我自己写的这个可以拿走直接用
1:创建一个类继承dialog
public class MyDialog extends Dialog {
private Context context;
private String title;
private String confirmButtonText;
private String cacelButtonText;
private TextView tvTitle;
private TextView tvConfirm;
private TextView tvCancel;
private CheckBox isSele;
private ClickListenerInterface clickListenerInterface;
public interface ClickListenerInterface { //在外部使用的点击事件接口
public void doConfirm();//确定的事件
public void doCancel();//取消的事件
void setIsSelect(boolean isChecked);//cheBox的状态
}
public MyDialog(@NonNull Context context, String title, String confirmButtonText, String cacelButtonText) {
super(context, R.style.MyDialog);
this.context=context;
this.title = title;
this.confirmButtonText = confirmButtonText;
this.cacelButtonText = cacelButtonText;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
}
private void init() {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.confirm_dialog, null);
setContentView(view);
tvTitle = (TextView) view.findViewById(R.id.title);
tvConfirm = (TextView) view.findViewById(R.id.confirm);
tvCancel = (TextView) view.findViewById(R.id.cancel);
isSele = view.findViewById(R.id.cb_issele);
tvTitle.setText(title);
tvConfirm.setText(confirmButtonText);
tvCancel.setText(cacelButtonText);
tvConfirm.setOnClickListener(new clickListener());
tvCancel.setOnClickListener(new clickListener());
isSele.setOnCheckedChangeListener(new clickListener());
Window dialogWindow = getWindow();
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
lp.width = (int) (d.widthPixels * 0.8); // 高度设置为屏幕的0.6
dialogWindow.setAttributes(lp);
}
public void setClicklistener(ClickListenerInterface clickListenerInterface) {
this.clickListenerInterface = clickListenerInterface;
}
private class clickListener implements View.OnClickListener,CompoundButton.OnCheckedChangeListener {
@Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.confirm:
clickListenerInterface.doConfirm();
break;
case R.id.cancel:
clickListenerInterface.doCancel();
break;
}
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
clickListenerInterface.setIsSelect(isChecked);
}
};
}
xml文件
<?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/item_bg"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingTop="14dp"
android:textColor="@color/login_hint"
android:textSize="@dimen/text_size_18" />
<CheckBox
android:id="@+id/cb_issele"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="不在提示"
android:layout_margin="@dimen/dp_10"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="14dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="30dp" >
<TextView
android:id="@+id/confirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:gravity="center"
android:textColor="@color/login_hint"
android:textSize="16sp" />
<TextView
android:id="@+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:gravity="center"
android:textColor="@color/login_hint"
android:textSize="16sp" />
</LinearLayout>
</LinearLayout>
不能忘记样式style
<style name="MyDialog" parent="@android:style/Theme.Holo.Dialog">
<!-- 是否有边框 -->
<item name="android:windowFrame">@null</item>
<!--是否在悬浮Activity之上 -->
<item name="android:windowIsFloating">true</item>
<!-- 标题 -->
<item name="android:windowNoTitle">true</item>
<!--阴影 -->
<item name="android:windowIsTranslucent">true</item><!--半透明-->
<!--背景透明-->
<item name="android:windowBackground">@android:color/transparent</item>
<!-- 还可以加入一些弹出和退出的动画 (lan)-->
</style>
这样基本就好了,现在开始使用,在需要的展示的地方使用直接new就好
if (!isSelect){//设置一个Boolean值来判断是否显示dialog,如果点了不在提示则不显示否则显示
final MyDialog myDialog = new MyDialog(GlidActivity.this,"补发试卷","立即补发","下次再说",false);
myDialog.show();
myDialog.setClicklistener(new MyDialog.ClickListenerInterface() {
@Override
public void doConfirm() {//点击确定的操作,
Toast.makeText(GlidActivity.this,"补发完成",Toast.LENGTH_LONG).show();
myDialog.dismiss();
}
@Override
public void doCancel() {
Toast.makeText(GlidActivity.this,"再见",Toast.LENGTH_LONG).show();
myDialog.dismiss();
}
@Override
public void setIsSelect(boolean isChecked) {
if (isChecked){
isSelect = isChecked;
Toast.makeText(GlidActivity.this,"勾选",Toast.LENGTH_LONG).show();
}else {
isSelect = isChecked;
Toast.makeText(GlidActivity.this,"未勾选",Toast.LENGTH_LONG).show();
}
}
});
}
}
这下就好了,可以使用了。
一部分内容取自这篇博客,大家有兴趣也可以看看https://www.cnblogs.com/lwbqqyumidi/p/3645561.html