码迷,mamicode.com
首页 > 微信 > 详细

自定义AlertDialog(仿微信)

时间:2015-04-24 12:40:11      阅读:291      评论:0      收藏:0      [点我收藏+]

标签:alertdialog

     安卓自定义AlertDialog,原理很简单:

AlertDialog dialog = new AlertDialog.Builder(MainActivity.this).create();
				dialog.show();
				Window window = dialog.getWindow();
				window.setContentView(R.layout.alert_dialog);


通过window设置自定义dialog布局;

效果如图:

技术分享

 

主界面MainActivity代码:

package com.byl.testalertdialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
	
	AlertDialog dialog=null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		dialog = new AlertDialog.Builder(MainActivity.this).create();
		
		Button btn = (Button) findViewById(R.id.btn);
		
		btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				dialog.show();
				Window window = dialog.getWindow();
				window.setContentView(R.layout.alert_dialog);
				//选项1
                ((TextView)window.findViewById(R.id.tv_item1)).setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View arg0) {
						Toast.makeText(getApplicationContext(), "选项1", Toast.LENGTH_LONG).show();
						dialog.cancel();
					}
				});
              //选项2
                ((TextView)window.findViewById(R.id.tv_item2)).setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View arg0) {
						Toast.makeText(getApplicationContext(), "选项2", Toast.LENGTH_LONG).show();
						dialog.cancel();
					}
				});
			}
		});

	}

}


源码下载地址http://download.csdn.net/detail/baiyuliang2013/8627229

自定义AlertDialog(仿微信)

标签:alertdialog

原文地址:http://blog.csdn.net/baiyuliang2013/article/details/45243707

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