android自定義對話框

效果圖

主要代碼:

package cn.bzu.dialogcase;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.support.v4.app.NavUtils;

public class MainActivity extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
		alertDialog
				.setTitle("登錄提示")
				.setMessage("這裏需要登錄")
				.setPositiveButton("確定", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// 點擊確定進入登錄對話框
						// Inflator在android中建立了資源文件到對象的橋樑
						LayoutInflater layoutInflater = LayoutInflater
								.from(MainActivity.this);
						// 得到自定義的對話框
						View dialogView = layoutInflater.inflate(
								R.layout.dialog, null);
						// 創建對話框
						AlertDialog.Builder loginDialog = new AlertDialog.Builder(
								MainActivity.this);
						loginDialog
								.setTitle("登錄框")
								.setView(dialogView)
								.setPositiveButton("確定",
										new DialogInterface.OnClickListener() {

											@Override
											public void onClick(
													DialogInterface dialog,
													int which) {
												// 輸入完成後,點擊確定開始登錄
												// 創建進度對話框
												final ProgressDialog progressDialog = ProgressDialog
														.show(MainActivity.this,
																"請等待",
																"正在爲你登錄", true);
												new Thread() {
													@Override
													public void run() {
														try {
															Thread.sleep(3000);
														} catch (InterruptedException e) {
															e.printStackTrace();
														} finally {
															progressDialog
																	.dismiss();
														}
													}
												}.start();

											}
										})
								.setNegativeButton("取消",
										new DialogInterface.OnClickListener() {

											@Override
											public void onClick(
													DialogInterface dialog,
													int which) {
												finish();// 如果點擊了取消按鈕則退出應用程序
											}
										}).create().show();

					}
				})
				.setNeutralButton("退出", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						finish();
					}
				}).create().show();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

}


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章