弹窗之三:自定义Dialog
16lz
2021-01-26
第一步: 给Dialog设置一个风格主题
(基本都是用这个主题)无边框全透明背景:
res/values/styles:
--自定义dialog背景全透明无边框theme -->
第二步:给自定义的Dialog设置自定义的 xml界面
比如:title+message+确定按钮、取消按钮
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="260dp" android:layout_height="wrap_content" android:layout_centerInParent="true" android:background="@drawable/dialog_bg" android:orientation="vertical"> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="15dp" android:gravity="center" android:text="消息提示" android:textColor="#38ADFF" android:textSize="16sp" /> <TextView android:id="@+id/message" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:text="提示消息" /> <View android:layout_width="match_parent" android:layout_height="1px" android:layout_marginTop="15dp" android:background="#E4E4E4" /> <LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:orientation="horizontal"> <Button android:id="@+id/no" android:layout_width="0dp" android:layout_height="match_parent" android:layout_marginLeft="10dp" android:layout_weight="1" android:background="@null" android:gravity="center" android:singleLine="true" android:text="No" android:textColor="#7D7D7D" android:textSize="16sp" /> <View android:layout_width="1px" android:layout_height="match_parent" android:background="#E4E4E4" /> <Button android:id="@+id/yes" android:layout_width="0dp" android:layout_height="match_parent" android:layout_marginRight="10dp" android:layout_weight="1" android:background="@null" android:gravity="center" android:singleLine="true" android:text="Yes" android:textColor="#38ADFF" android:textSize="16sp" /> LinearLayout> LinearLayout>RelativeLayout>
dialog_bg.xml如下:
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#ffffff" /> <corners android:radius="6dp" />shape>
第三步:继承Dialog实现自定义的Dialog
public class CustomDialog extends Dialog { private Button yes; private Button no; private TextView titleTv; private TextView messageTv; private String titleStr; private String messageStr; private String yesStr, noStr; /* -------------------------------- 接口监听 ------------------------------------- */ private onNoOnclickListener noOnclickListener; private onYesOnclickListener yesOnclickListener; public interface onYesOnclickListener { void onYesClick(); } public interface onNoOnclickListener { void onNoClick(); } public void setNoOnclickListener(String str, onNoOnclickListener onNoOnclickListener) { if (str != null) { noStr = str; } this.noOnclickListener = onNoOnclickListener; } public void setYesOnclickListener(String str, onYesOnclickListener onYesOnclickListener) { if (str != null) { yesStr = str; } this.yesOnclickListener = onYesOnclickListener; } /* ---------------------------------- 构造方法 ------------------------------------- */ public CustomDialog(Context context) { super(context, R.style.MyDialog);//风格主题 } /* ---------------------------------- onCreate------------------------------------- */ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.custom_dialog_layout);//自定义布局 //按空白处不能取消动画// setCanceledOnTouchOutside(false); //初始化界面控件 initView(); //初始化界面数据 initData(); //初始化界面控件的事件 initEvent(); } /** * 初始化界面的确定和取消监听器 */ private void initEvent() { //设置确定按钮被点击后,向外界提供监听 yes.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (yesOnclickListener != null) { yesOnclickListener.onYesClick(); } } }); //设置取消按钮被点击后,向外界提供监听 no.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (noOnclickListener != null) { noOnclickListener.onNoClick(); } } }); } /** * 初始化界面控件的显示数据 */ private void initData() { //如果用户自定了title和message if (titleStr != null) { titleTv.setText(titleStr); } if (messageStr != null) { messageTv.setText(messageStr); } //如果设置按钮的文字 if (yesStr != null) { yes.setText(yesStr); } if (noStr != null) { no.setText(noStr); } } /** * 初始化界面控件 */ private void initView() { yes = (Button) findViewById(R.id.yes); no = (Button) findViewById(R.id.no); titleTv = (TextView) findViewById(R.id.title); messageTv = (TextView) findViewById(R.id.message); } /* ---------------------------------- set方法 传值------------------------------------- *///为外界设置一些public 公开的方法,来向自定义的dialog传递值 public void setTitle(String title) { titleStr = title; } public void setMessage(String message) { messageStr = message; }}
第四步:activity中调用:
final CustomDialog customDialog = new CustomDialog(MainActivityI.this); customDialog.setTitle("提示"); customDialog.setMessage("确定退出应用?"); customDialog.show(); customDialog.setYesOnclickListener("确定", new CustomDialog.onYesOnclickListener() { @Override public void onYesClick() { Toast.makeText(MainActivityI.this, "点击了--确定--按钮", Toast.LENGTH_LONG).show(); customDialog.dismiss(); } }); customDialog.setNoOnclickListener("取消", new CustomDialog.onNoOnclickListener() { @Override public void onNoClick() { Toast.makeText(MainActivityI.this, "点击了--取消--按钮", Toast.LENGTH_LONG).show(); customDialog.dismiss(); } });
效果如图:
参考:自定义Dialog的简单实现
1、设置标题和内容可以使用构造方法
2、监听确定和取消事件可以使用一个接口:
如下
1、dialog布局:
注意最外层的宽度为match_parent,方便后面修改宽度
<?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="wrap_content" android:layout_centerInParent="true" android:background="@drawable/dialog_bg" android:orientation="vertical"> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="15dp" android:gravity="center" android:text="消息提示" android:textColor="#38ADFF" android:textSize="16sp" /> <TextView android:id="@+id/message" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:text="提示消息" /> <View android:layout_width="match_parent" android:layout_height="1px" android:layout_marginTop="15dp" android:background="#E4E4E4" /> <LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:orientation="horizontal"> <Button android:id="@+id/no" android:layout_width="0dp" android:layout_height="match_parent" android:layout_marginLeft="10dp" android:layout_weight="1" android:background="@null" android:gravity="center" android:singleLine="true" android:text="No" android:textColor="#7D7D7D" android:textSize="16sp" /> <View android:layout_width="1px" android:layout_height="match_parent" android:background="#E4E4E4" /> <Button android:id="@+id/yes" android:layout_width="0dp" android:layout_height="match_parent" android:layout_marginRight="10dp" android:layout_weight="1" android:background="@null" android:gravity="center" android:singleLine="true" android:text="Yes" android:textColor="#38ADFF" android:textSize="16sp" /> LinearLayout>LinearLayout>
2、定义dialog:
public class MyDialog extends Dialog { private Context context; private String message; private String title; private String confirmButtonText; private String cacelButtonText; /* ----------------------------------接口监听---------------------------------------- */ private ClickListenerInterface clickListenerInterface; public interface ClickListenerInterface { void doConfirm(); void doCancel(); } public void setClickListener(ClickListenerInterface clickListenerInterface) { this.clickListenerInterface = clickListenerInterface; } /* ----------------------------------构造方法:传值------------------------------------- */ public MyDialog(Context context, String title, String message, String confirmButtonText, String cacelButtonText) { super(context, R.style.MyDialog); this.context = context; this.title = title; this.message = message; this.confirmButtonText = confirmButtonText; this.cacelButtonText = cacelButtonText; } /* ---------------------------------- onCreate ------------------------------------- */ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); init(); } @SuppressWarnings("ConstantConditions") @SuppressLint("InflateParams") public void init() { setContentView(R.layout.my_dialog); TextView tvTitle = (TextView) findViewById(R.id.title); TextView tvMessage = (TextView) findViewById(R.id.message); TextView tvConfirm = (TextView) findViewById(R.id.yes); TextView tvCancel = (TextView) findViewById(R.id.no); tvTitle.setText(title); tvMessage.setText(message); tvConfirm.setText(confirmButtonText); tvCancel.setText(cacelButtonText); tvConfirm.setOnClickListener(new clickListener()); tvCancel.setOnClickListener(new clickListener()); setCanceledOnTouchOutside(false);//设置窗口外是否可关闭 setOnKeyListener(new DialogInterface.OnKeyListener() {//设置按back键不关闭 @Override public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) { return keyCode == KeyEvent.KEYCODE_BACK; } }); Window dialogWindow = getWindow(); if (dialogWindow != null) { dialogWindow.setGravity(Gravity.BOTTOM | Gravity.CENTER);//设置窗口位置 dialogWindow.setWindowAnimations(R.style.dialogWindowAnim); //设置窗口进出动画 WindowManager.LayoutParams lp = dialogWindow.getAttributes(); DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高 if (lp != null) { lp.width = (int) (d.widthPixels * 0.7); // 设置为屏幕宽度 } dialogWindow.setAttributes(lp); } } private class clickListener implements View.OnClickListener { @Override public void onClick(View v) { switch (v.getId()) { case R.id.yes: clickListenerInterface.doConfirm(); break; case R.id.no: clickListenerInterface.doCancel(); break; } } }}
3、activity调用:
final MyDialog myDialog = new MyDialog(MainActivityI.this, "提示", "确定要退出吗?", "退出", "取消"); myDialog.show(); myDialog.setClickListener(new MyDialog.ClickListenerInterface() { @Override public void doConfirm() { Toast.makeText(MainActivityI.this, "点击了--确定--按钮", Toast.LENGTH_LONG).show(); myDialog.dismiss(); } @Override public void doCancel() { Toast.makeText(MainActivityI.this, "点击了--取消--按钮", Toast.LENGTH_LONG).show(); myDialog.dismiss(); } });
4、动画:
res/values/styles:
<style name="dialogWindowAnim" mce_bogus="1" parent="android:Animation"> <item name="android:windowEnterAnimation">@anim/animator_dialog_in - "android:windowExitAnimation"
>@anim/animator_dialog_out style>
animator_dialog_in
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="400" android:fromXDelta="100%" android:toXDelta="0" />set>
animator_dialog_out
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="400" android:fromXDelta="0" android:toXDelta="-100%" />set>
效果如图:
参考:Android自定义Dialog
其他:
如果对话框需要添加动画,推荐使用dialogFragment,自定义dialog添加动画,在按home键后会自动执行动画。监听back键相同。
更多相关文章
- Android音乐播放器【支持:速率调节,音调调节,采样率调节】
- Android(安卓)Studio 入门:(五) 悬浮按钮增加联系人信息
- android中editText弹出软键盘并且根据editText中是否是内容来控
- 仿QQ设置字体大小自定义SeekBar
- android BitmapFactory.Options参数介绍
- Hardcoded string "下一步", should use @string resource警告
- Android(安卓)RadioGroup中设置默认选中RadioButton 后,选中两个
- android 利用FloatActionButton悬浮按钮实现扇形折叠与隐藏
- 【转】Windows下设置Android模拟器上网