1,继承自dialog
public class DialogChoice02 extends Dialog
2,重写构造方法,自定义style(定义dialog弹窗动画,背景等)
public DialogChoice02(@NonNull Context context) {
super(context, R.style.DialogScaleStyle);
}
style:
<style name="DialogScaleStyle" parent="android:style/Theme.Dialog">
<!--内容区域背景色-->
<item name="android:background">@android:color/transparent</item>
<item name="android:windowBackground">@android:color/transparent</item>
<!--dialog动画-->
<item name="android:windowAnimationStyle">@style/MyDialogAnimStyle</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<!--设置弹窗全屏背景是否变暗,默认true-->
<!--<item name="android:backgroundDimEnabled">false</item>-->
</style>
dialog 显示消失动画
<style name="MyDialogAnimStyle" parent="android:Animation">
<item name="android:windowEnterAnimation">@anim/dialog_in</item>
<item name="android:windowExitAnimation">@anim/dialog_out</item>
</style>
anmi:in
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:pivotX="50%"
android:pivotY="50%"
android:fromXScale="0"
android:fromYScale="0"
android:toXScale="1"
android:toYScale="1"
android:duration="300"/>
<alpha
android:duration="300"
android:fromAlpha="0"
android:toAlpha="1"
/>
</set>
anmi:out
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:fromXScale="1"
android:fromYScale="1"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="0"
android:toYScale="0"
android:duration="200"/>
<alpha
android:duration="200"
android:fromAlpha="1"
android:toAlpha="0"
/>
</set>
3,重写onCreate 在dialog创建时只执行一次,,如果要实时刷新dialog信息,另写更新UI的方法
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_choice);
//按空白处不能取消动画
setCanceledOnTouchOutside(false);
//初始化界面控件
initView();
//初始化界面数据
initData();//dialog创建时只会执行一次
//初始化界面控件的事件
initEvent();
}
private void initView() {
title = (TextView) findViewById(R.id.title);
message = (TextView) findViewById(R.id.message);
positiveButton = (Button) findViewById(R.id.positiveButton);
negativeButton = (Button) findViewById(R.id.negativeButton);
}
private void initData() {
//如果用户自定了title和message
if (titleStr != null) {
title.setVisibility(View.VISIBLE);
title.setText(titleStr);
}
if (messageStr != null) {
message.setVisibility(View.VISIBLE);
message.setText(messageStr);
}
//如果设置按钮的文字
if (yesStr != null) {
positiveButton.setText(yesStr);
}
if (noStr != null) {
negativeButton.setText(noStr);
}
}
private void initEvent() {
//默认前面一个选中焦点,最好在外面再调用初始化焦点
positiveButton.requestFocus();
positiveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (yesOnclickListener != null) {
yesOnclickListener.onYesClick();
}
dismiss();
}
});
negativeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (noOnclickListener != null) {
noOnclickListener.onNoClick();
}
dismiss();
}
});
}
/**
* 从外界Activity为Dialog设置标题
*
* @param titleText
*/
public void setTitle(String titleText) {
titleStr = titleText;
if (title != null && titleStr != null) {
title.setVisibility(View.VISIBLE);
title.setText(titleStr);
}
}
/**
* 从外界Activity为Dialog设置dialog的message
*
* @param messageText
*/
public void setMessage(String messageText) {
messageStr = messageText;
if (message != null && messageStr != null) {
message.setVisibility(View.VISIBLE);
message.setText(messageStr);
}
}
4,设置确定按钮和取消被点击的接口
public interface onYesOnclickListener {
void onYesClick();
}
public interface onNoOnclickListener {
void onNoClick();
}
/**
* 设置取消按钮的显示内容和监听
* @param str
* @param onNoOnclickListener
*/
public void setNoOnclickListener(String str, onNoOnclickListener onNoOnclickListener) {
if (str != null) {
noStr = str;
}
if (noStr != null && negativeButton != null) {
negativeButton.setText(noStr);
}
this.noOnclickListener = onNoOnclickListener;
}
/**
* 设置确定按钮的显示内容和监听
* @param str
* @param onYesOnclickListener
*/
public void setYesOnclickListener(String str, onYesOnclickListener onYesOnclickListener) {
if (str != null) {
yesStr = str;
}
if (yesStr != null && positiveButton != null) {//防止该方法在onCreate后执行(log是在前面执行的,有点不可思议)
positiveButton.setText(yesStr);
}
this.yesOnclickListener = onYesOnclickListener;
}
5,初始化焦点,部分电视上,需要代码拿焦点,不会自己取焦点
public void initFocus(boolean focusPos) {
if (positiveButton == null) return;
if (negativeButton == null) return;
if (focusPos) {
positiveButton.requestFocus();
} else {
negativeButton.requestFocus();
}
}
6,外部调用
/**
* 退出对话框
*/
private DialogChoice02 dialogChoice02;
private void showExitDialog() {
if (dialogChoice02 == null)
dialogChoice02 = new DialogChoice02(mContext);
dialogChoice02.setTitle("即将退出每家相册");
dialogChoice02.setYesOnclickListener("残忍离开", new DialogChoice02.onYesOnclickListener() {
@Override
public void onYesClick() {
BrowserCallback.getInstance().exit();
ImAidlManage.getInstance().exit();
MyApplication.getInstance().exit();
}
});
dialogChoice02.setNoOnclickListener("留下看看", new DialogChoice02.onNoOnclickListener() {
@Override
public void onNoClick() {
}
});
dialogChoice02.initFocus(true);
dialogChoice02.show();
}
//设置dialog显示的位置
Window window =dialogChoice02.getWindow();
window.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL);
//上面两句可以设置显示的大概位置上下左右, 居中。
//下面的可以设置dialog具体在屏幕的坐标
WindowManager.LayoutParams params = window.getAttributes();
params.x = 10; 设置x坐标【取绝对值,负值无效】
params.y = 100; //设置y坐标
params.width = 220; //dialog宽
params.height = 200; //dialog高
window.setAttributes(params);
放图:
圆角shape 选中效果用selector实现。