自定义AlertDialog(仿微信)

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

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

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

效果如图:

自定义AlertDialog(仿微信)

主界面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

上一篇:oracle中斜杠(/)的含义


下一篇:管理Android设备的唤醒状态