Android AlertDialog.Builder进入和退出动画效果设置

Android中,最常的对话框估计就是AlertDialog.Builder了,下面学习一下怎么给它加一些特殊动画效果。

/**
	 * 弹出退出对话框
	 */
	private void showExitDialog() {
		AlertDialog.Builder builder = new Builder(MainActivity.this);
		builder.setTitle("退出");
		builder.setMessage("确定要退出吗?");
		builder.setNegativeButton("取消", null);
		builder.setNeutralButton("支持", null);
		builder.setPositiveButton("退出", new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// 停止服务
				stopService(new Intent(MainActivity.this, MainService.class));
				// 结束当前窗体
				MainActivity.this.finish();
			}
		});
		<strong><span style="color:#ff0000;">AlertDialog dialog = builder.create();
</span></strong>		dialog.show();
		Window window = dialog.getWindow();
		window.setWindowAnimations(R.style.<strong><span style="color:#33cc00;">dialog_anim</span></strong>);
	}
@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			showExitDialog();
		}
		return true;
	}


当用户在按下手机上的返回键是,就会弹出该对话框。该对话框会根据自己所设计的方法弹出来,我设置的是进入时从下往上显示在屏幕中,退出时从屏幕中向下退出。

来看看style文件中的配置:

<style name="dialog_anim" parent="@android:style/Animation">
        <item name="android:windowEnterAnimation">@anim/dialog_appear</item>
        <item name="android:windowExitAnimation">@anim/dialog_disappear</item>
    </style>

其中,anim文件夹下有dialog_appear和dialog_disappear两个资源文件

res/anim/dialog_appear.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="500"
        android:fromYDelta="100.0%p" />

    <alpha
        android:duration="500"
        android:fromAlpha="0.5"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toAlpha="1.0" />

</set>

res/anim/dialog_disappear.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="500"
        android:toYDelta="100.0%p" />

    <alpha
        android:duration="500"
        android:fromAlpha="0.5"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toAlpha="1.0" />

</set>


如此,在点击返回按键时,即可看到一个对话框从下往上的显示在屏幕上了大笑

 

另外,给AlertDialog设置View,当用户点击view中的按键时,同时结束该对话框。

/**
	 * 显示搜索对话框
	 */
	private void showDialogBySearch() {
		<span style="color:#ff0000;">final AlertDialog dialog = new AlertDialog.Builder(this).create();
		dialog.show();
</span>		Window window = dialog.getWindow();
		window.setWindowAnimations(R.style.dialog_anim);
		window.setContentView(<span style="BACKGROUND-COLOR: #33ff33">R.layout.search_dialog</span>);
		final RadioGroup rg = (RadioGroup) <span style="BACKGROUND-COLOR: #99ff99">window.</span>findViewById(R.id.radioGroup1);
		final EditText et_word = (EditText) window.findViewById(R.id.et_word);
		Button btn_search = (Button) window.findViewById(R.id.btn_search);
		btn_search.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				search(rg, et_word);
				<span style="color:#ff0000;">dialog.dismiss();
</span>			}

			public void search(final RadioGroup rg, final EditText et_word) {
				List<Poem> list = null;
				String word = et_word.getText().toString().trim();
				int rbId = rg.getCheckedRadioButtonId();
				switch (rbId) {
				case R.id.radio0: // 标题
					list = poemDAO.getGeneral("title", word);
					break;
				case R.id.radio1: // 诗歌
					list = poemDAO.getGeneral("content", word);
					break;
				case R.id.radio2: // 注解
					list = poemDAO.getGeneral("desc", word);
					break;
				}
				MyAdapter adapter = new MyAdapter(MainActivity.this, list);
				lv_catelog.setAdapter(adapter);
			}
		});
	}

res/layout/search_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:background="#fff"
    android:orientation="vertical" >

    <RadioGroup
        android:id="@id/radioGroup1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="12.0dip"
        android:gravity="center"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@id/radio0"
            android:layout_width="0.0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:checked="true"
            android:text="@string/str20149" />

        <RadioButton
            android:id="@id/radio1"
            android:layout_width="0.0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:text="@string/str20150" />

        <RadioButton
            android:id="@id/radio2"
            android:layout_width="0.0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:text="@string/str20151" />
    </RadioGroup>

    <EditText
        android:id="@id/et_word"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="12.0dip"
        android:ems="10"
        android:hint="请输入查询关键字"
        android:singleLine="true" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@id/btn_search"
        android:layout_width="140.0dip"
        android:layout_height="wrap_content"
        android:layout_marginBottom="12.0dip"
        android:text="@string/str20152" />

</LinearLayout>



 

当点击自定义View内面的查询按钮后,对话框就会退出页面了。。。。。。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值