【发布时间】:2019-10-27 13:11:08
【问题描述】:
我正在尝试向警报对话框添加动画,当用户触摸它外部时它被关闭。默认情况下,当它在外面被触摸时,对话框只是淡出或消失。有没有办法覆盖默认的解雇或取消?我能够在按下按钮和调用按钮时添加动画,但无法弄清楚如何在外部触摸按钮时将动画添加到默认关闭操作中。请帮忙。提前致谢
我试过了,还是不行
alert.setOnCancelListener()
alert.setOnDismissListener()
这是我的代码:
View rl = getActivity().findViewById(R.id.map) ;
AlertDialog.Builder alert = new
AlertDialog.Builder(rl.getContext(), R.style.CFDialog);
LayoutInflater inflater =
getActivity().getLayoutInflater();
View v =
inflater.inflate(R.layout.dialog_footer_layout, null);
Animation transition_in_view =
AnimationUtils.loadAnimation(rl.getContext(),
R.anim.alert_present);
Animation transition_out_view =
AnimationUtils.loadAnimation(rl.getContext(),
R.anim.alert_dismiss);
//customer animation appearance
v.setAnimation( transition_in_view );
v.startAnimation( transition_in_view );
alert.setView(v);
alert.setOnCancelListener(new
DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface
dialogInterface) {
v.startAnimation(transition_out_view);
}
});
alert.setOnDismissListener()
Button button =
v.findViewById(R.id.configuration_toggle_button);
AlertDialog helpDialog = alert.create();
button.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View view) {
v.startAnimation(transition_out_view);
transition_out_view.setAnimationListener(new
Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation
animation) {
}
@Override
public void onAnimationEnd(Animation
animation) {
helpDialog.dismiss();
}
@Override
public void onAnimationRepeat(Animation
animation) {
}
});
}
});
helpDialog.setOnCancelListener(new
DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface
dialogInterface) {
v.startAnimation(transition_out_view);
return;
}
});
helpDialog.setOnDismissListener(new
DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface
dialogInterface) {
v.startAnimation(transition_out_view);
return;
}
});
// Hide after some seconds
final Handler handler = new Handler();
final Runnable runnable = new Runnable() {
@Override
public void run() {
if (helpDialog.isShowing()) {
v.startAnimation(transition_out_view);
transition_out_view.setAnimationListener(new
Animation.AnimationListener() {
@Override
public void
onAnimationStart(Animation animation) {
}
@Override
public void
onAnimationEnd(Animation animation) {
helpDialog.dismiss();
}
@Override
public void
onAnimationRepeat(Animation animation) {
}
});
}
}
};
alert.setOnDismissListener(new
DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
handler.removeCallbacks(runnable);
}
});
handler.postDelayed(runnable, 5000);
helpDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
WindowManager.LayoutParams wmlp =
helpDialog.getWindow().getAttributes();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
wmlp.gravity = Gravity.TOP;
wmlp.y = 200; //y position
helpDialog.show();
} else {
// if (Build.VERSION.SDK_INT <=
Build.VERSION_CODES.O)
wmlp.gravity = Gravity.TOP;
wmlp.y = 180; //y position
helpDialog.show();
}
这是对话框的样式:
<style name="CFDialog" parent="Theme.AppCompat.Translucent">
<item name="android:backgroundDimEnabled">false</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimAmount">0.5</item>
<item name="colorPrimaryDark">@color/transparent</item>
<item name="android:windowMinWidthMajor">100%</item>
<item name="android:windowMinWidthMinor">100%</item>
【问题讨论】:
标签: android android-animation android-alertdialog dismiss