码迷,mamicode.com
首页 > 其他好文 > 详细

自定义AlertDialog(圆角+退出动画+自定义布局)

时间:2015-05-09 06:38:38      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:alertdialog 自定义 圆角 动

图片省略了不上传了。。。

首先看activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="点击我,弹出AlertDialog" />

</RelativeLayout>
MainActivity.java

package com.godinsec.alertdialog;

import com.godinsec.alertdialog.ShowDeleDialog.onBtnClickListener;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				new ShowDeleDialog(MainActivity.this, new onBtnClickListener(){

					@Override
					public void onSure() {
						Toast.makeText(MainActivity.this, "进行确认操作", 0).show();
					}

					@Override
					public void onExit() {
						Toast.makeText(MainActivity.this, "进行取消操作", 0).show();
					}
					
				}, "确定", "取消", "提示", "AlertDialog的内容").show();
			}
		});
	}

}

接下来看自定义alertDialog

dele_allcontacts_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <RelativeLayout
        android:id="@+id/re_tip_dialog"
        android:layout_width="300dp"
        android:layout_height="120dp"
		android:layout_centerInParent="true"
        android:background="@drawable/rounded_search_text">

        <TextView
            android:id="@+id/appTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:text="标题"/>

        <TextView
            android:id="@+id/tips"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/appTitle"
            android:layout_alignLeft="@id/appTitle"
            android:text="AlertDialog的内容"
            android:layout_marginTop="20dp"/>

        <TextView
            android:id="@+id/sure"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="确认"
            android:layout_below="@id/tips"
            android:layout_marginTop="20dp" />

        <TextView
            android:id="@+id/cancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@id/sure"
            android:text="取消"
            android:layout_marginRight="30dp"
            android:layout_toLeftOf="@id/sure" />
    </RelativeLayout>

</RelativeLayout>
rounded_search_text.xml(圆角)

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

    <solid android:color="#FF7F24" />

    <corners
        android:bottomLeftRadius="4dp"
        android:bottomRightRadius="4dp"
        android:topLeftRadius="4dp"
        android:topRightRadius="4dp" />

</shape>
ShowDeleDialog.java

package com.godinsec.alertdialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.ScaleAnimation;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ShowDeleDialog extends Dialog implements
		android.view.View.OnClickListener {
	private onBtnClickListener onBtnClickListener;
	private Context context;

	private String sure;
	private String cancle;
	private String appTitle;
	private String tips;

	private TextView tv_dele_sure;
	private TextView tv_dele_cancle;
	private TextView tv_appTitle;
	private TextView tv_tips;

	private RelativeLayout re_tip_dialog;

	public ShowDeleDialog(Context context,
			onBtnClickListener onBtnClickListener, String sure, String cancle,
			String appTitle, String tips) {
		super(context);
		this.onBtnClickListener = onBtnClickListener;
		this.context = context;
		this.appTitle = appTitle;
		this.tips = tips;
		this.sure = sure;
		this.cancle = cancle;

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 去除默认的头部标题
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.dele_allcontacts_dialog);
		tv_dele_sure = (TextView) findViewById(R.id.sure);
		tv_dele_cancle = (TextView) findViewById(R.id.cancel);
		tv_appTitle = (TextView) findViewById(R.id.appTitle);
		tv_tips = (TextView) findViewById(R.id.tips);
		re_tip_dialog = (RelativeLayout) findViewById(R.id.re_tip_dialog);

		tv_dele_cancle.setOnClickListener(this);
		tv_dele_sure.setOnClickListener(this);

		Window window = this.getWindow();
		WindowManager.LayoutParams params = this.getWindow().getAttributes();
		// 去除四角黑色背景
		window.setBackgroundDrawable(new BitmapDrawable());
		// 设置周围的暗色系数
		params.dimAmount = 0.5f;
		window.setAttributes(params);

		// 为各个textview赋值
		tv_dele_sure.setText(sure);
		tv_dele_cancle.setText(cancle);
		tv_appTitle.setText(appTitle);
		tv_tips.setText(tips);
	}

	public interface onBtnClickListener {
		public void onSure();

		public void onExit();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.cancel:

			onBtnClickListener.onExit();

			alertDialogExitAnim();
			break;
		case R.id.sure:
			onBtnClickListener.onSure();
			alertDialogExitAnim();
			break;

		default:
			break;
		}
	}

	private void alertDialogExitAnim() {
		ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 0.0f,
				1.0f, 0.0f, Animation.ABSOLUTE,
				re_tip_dialog.getWidth() / 2, Animation.ABSOLUTE,
				re_tip_dialog.getHeight() / 2);
		scaleAnimation.setDuration(1000);
		re_tip_dialog.startAnimation(scaleAnimation);

		scaleAnimation.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {

			}

			@Override
			public void onAnimationRepeat(Animation animation) {

			}

			@Override
			public void onAnimationEnd(Animation animation) {
				ShowDeleDialog.this.dismiss();
			}
		});
	}

}





自定义AlertDialog(圆角+退出动画+自定义布局)

标签:alertdialog 自定义 圆角 动

原文地址:http://blog.csdn.net/u013210620/article/details/45591571

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!