AlertDialog添加动画

创建AlertDialog(这里使用自定义布局)

java代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
View inflate = View.inflate(getContext(), R.layout.my_module_dialog_confirmthegoods, null);
builder.setView(inflate);
final AlertDialog alertDialog = builder.create();
final EditText myModuleDialogConfirmthegoodsEtGoodscode= (EditText) inflate.findViewById(R.id.my_module_dialog_confirmthegoods_et_goodscode);
TextView myModuleDialogConfirmthegoodsTvCancle= (TextView) inflate.findViewById(R.id.my_module_dialog_confirmthegoods_tv_cancle);
final TextView myModuleDialogConfirmthegoodsTvOk= (TextView) inflate.findViewById(R.id.my_module_dialog_confirmthegoods_tv_ok);
myModuleDialogConfirmthegoodsTvCancle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alertDialog.dismiss();
}
});
myModuleDialogConfirmthegoodsTvOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String goodcode = GetTextUtil.getText(myModuleDialogConfirmthegoodsEtGoodscode);
if(TextUtils.isEmpty(goodcode)){
ToastUtils.showShort("请输入发货码");
return;
}
alertDialog.dismiss();
}
});
alertDialog.show();

xml布局

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:orientation="vertical"
android:padding="15dp"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:textSize="17sp"
android:text="提示"
android:layout_marginLeft="5dp"
android:textColor="#000"
android:layout_height="wrap_content" />
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_marginTop="10dp"
app:counterEnabled="true"
app:counterMaxLength="7"
app:counterOverflowTextAppearance="@style/HintError"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:hint="请输入收货码"
android:textSize="17sp"
android:id="@+id/my_module_dialog_confirmthegoods_et_goodscode"
android:layout_height="wrap_content" />
</android.support.design.widget.TextInputLayout>
<LinearLayout
android:layout_width="wrap_content"
android:orientation="horizontal"
android:layout_gravity="right"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:text="取消"
android:id="@+id/my_module_dialog_confirmthegoods_tv_cancle"
android:textColor="@color/colorDialogBt"
android:textSize="15sp"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:text="确认"
android:id="@+id/my_module_dialog_confirmthegoods_tv_ok"
android:textColor="@color/colorDialogBt"
android:layout_marginLeft="20dp"
android:textSize="15sp"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>

获取Window对象并设置动画

1
2
Window window = alertDialog.getWindow();
window.setWindowAnimations(R.style.CustomDialog); //添加动画

style

1
2
3
4
<style name="CustomDialog" type="text/css">
<item name="android:windowEnterAnimation">@anim/dialog_enter</item>
<item name="android:windowExitAnimation">@anim/dialog_exit</item>
</style>

动画anim/dialog_enter

1
2
3
4
5
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="-100%p" android:duration="300"/>
<!--<scale android:fromXScale="0.5" android:duration="600"/>-->
</set>

动画anim/dialog_exit

1
2
3
4
5
6
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="300"
android:toXDelta="100%p" />
</set>

坚持原创技术分享,您的支持将鼓励我继续创作!
-------------本文结束感谢您的阅读-------------