`
oyqh
  • 浏览: 23675 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

AlertDialog.Builder()的应用及扩展

阅读更多
那我们先了解什么是AlertDialog?什么是AlertDialog.Builder?且两者有什么区别?

   AlertDialog是Dialog的一个直接子类,AlertDialog也是Android系统当中最常用的对话框之一。
   一个AlertDialog可以有两个以上的Button,可以对一个AlertDialog设置相应的信息。比如title,massage,setSingleChoiceItems,setPositiveButton,setNegativeButton等等。。。。

   但不能直接通过AlertDialog的构造函数来生产一个AlertDialog。研究AlertDialog的源码发现AlertDialog所有的构造方法都是写保护的所以不能通过:AlertDialog alertDialog  = new AlertDialog();来得到。AlertDialog构造方法源码如下:
    protected AlertDialog(Context context) {
        this(context, com.android.internal.R.style.Theme_Dialog_Alert);
    }

    protected AlertDialog(Context context, int theme) {
        super(context, theme);
        mAlert = new AlertController(context, this, getWindow());
    }

    protected AlertDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, com.android.internal.R.style.Theme_Dialog_Alert);
        setCancelable(cancelable);
        setOnCancelListener(cancelListener);
        mAlert = new AlertController(context, this, getWindow());
    }


只能通过:
 AlertDialog.Builder alertDialog  =new  AlertDialog.Builder(this);
来得到。


那就通过一个具体的实例来说说吧(这里用一个最常用的对话框为例):
package com.oyah;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;

public class TestsActivity extends Activity {
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
		alertDialog
				.setTitle("title")
				.setMessage("message")
				.setPositiveButton("okBuuon",
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {

							}
						})
				.setNegativeButton("exitButton",
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {

							}
						}).[b]setCancelable(false).[/b]create().show();
	}
}

针对AlertDialog中设置了确定和取消按钮,一般来说确定为执行某个动作,取消就是不执行,但是如果用户点击了系统的Back键,此时就会将AlertDialog关闭,而并没有执行预期的取消的操作。

此时需要关注一个方法setCancelable(false) 该方法定义设置该AlertDialog是否可以被Back键取消,如果不设置默认为true

下面是一些扩展:
根据AlertDialog.Builder 创建 相应的 AlertDialog

AlertDialog alertDialogs = alertDialog.create();


用dismiss();方法来清除创建过的AlertDialog

alertDialogs.dismiss();   


以上所采用的都是AlertDialog 系统默认的布局 现在说自定义布局的情况 并添加一个用于取消AlertDialog 的 Button

定义其布局 main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    >
<LinearLayout 
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >
 </LinearLayout>
</LinearLayout>


通过LayoutInflater 得到上面 main.xml 布局文件
view = this.getLayoutInflater().inflate(R.layout.main, null);


指定AlertDialog.Builder 所需的布局 并返回目标AlertDialog
alertDialog.setView(view);

alertDialogs= alertDialog.create();



通过 view.findViewById() 得到 目标View 然后设置其内容 如:
TextView title = (TextView) view.findViewById(R.id.title);
title.setTextSize(20);
title.setTextColor(Color.RED);
title.setText("Title");
        
TextView message = (TextView) view.findViewById(R.id.message);
message.setText("message");


显示对话框 AlertDialog

findViewById(R.id.button).setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				// TODO Auto-generated method stub
				alertDialogs.show();
			}
        });


清除对话框 AlertDialog

view.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				// TODO Auto-generated method stub
				alertDialogs.dismiss();
			}
        });
分享到:
评论
1 楼 1qqqqqq 2011-11-30  
恩 好文 明白了

相关推荐

    封装AlertDialog.Builder方法工程

    这个工程文件`AlertDialogBuilder`很可能包含了上述功能的扩展和封装,可能包括自定义主题、自定义布局、多按钮处理等。它可能会提供一些方便的方法,例如: - `showSimpleAlert(String title, String message)`:...

    Android中实现Iphone样式的AlertDialog.rar

    在这个主题中,可以设置窗口背景透明度、对话框边距、文字样式等属性,然后在创建`AlertDialog.Builder`时传入这个主题。 4. **自定义对话框样式** 自定义样式通常涉及修改`alert_dialog.xml`布局文件,该文件位于...

    安卓Dialog对话框相关-Android中实现Iphone样式的AlertDialog.zip

    AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle("Title") .setMessage("Message") .setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public ...

    Android应用源码之alertDialog-IT计算机-毕业设计.zip

    在Android应用开发中,AlertDialog是一个重要的组件,常用于向用户展示警告、确认或者提供选项。在毕业设计中,深入理解并运用AlertDialog可以帮助我们构建出更交互友好的移动应用。本项目是一个关于Android应用源码...

    安卓开发-Android中实现Iphone样式的AlertDialog.zip.zip

    在Android应用开发中,我们经常会遇到需要创建对话框(AlertDialog)的情况,以向用户展示一些重要信息或进行交互。在本教程中,我们将探讨如何在Android中实现与iPhone样式相似的AlertDialog,为用户提供更加一致的...

    安卓Andriod源码——中实现Iphone样式的AlertDialog.zip

    - 在AndroidManifest.xml中为应用或特定Activity定义一个新的主题,或者在代码中动态设置,使用`AlertDialog.Builder`的`setTheme()`方法。 - 调整主题的颜色,如背景色、按钮颜色、文字颜色等。可以使用颜色资源...

    自定义alertDialog

    本教程将围绕“自定义`AlertDialog`”这一主题,详细介绍如何在不编写自定义布局的情况下,在Android应用中创建符合需求的对话框。 首先,我们需要了解`AlertDialog.Builder`类,它是创建`AlertDialog`的主要工具。...

    打造Android通用的自定义AlertDialog提示框使用示例

    2. **使用Builder模式**: 创建一个自定义的`AlertDialog.Builder`类,扩展系统的`AlertDialog.Builder`,并添加自定义方法。 ```java public class CustomAlertDialogBuilder extends AlertDialog.Builder { ...

    Android AlertDialog 详解.docx

    而进度对话框则专门用于显示进度条或进度环,通常在后台任务执行时使用,它是AlertDialog的扩展形式,同样支持添加按钮。 显示AlertDialog有特定的步骤。首先,你需要在Activity的`onCreateDialog(int)`回调中创建...

    Android alertDialog对话框.zip源码资源下载

    - 创建`AlertDialog.Builder`对象。 - 使用`Builder`设置对话框的属性,如标题、消息、按钮等。 - 调用`create()`方法生成`AlertDialog`对象。 - 可选地,调用`show()`方法来显示对话框。 2. **Builder方法** ...

    Android之AlertDialog源码.

    另一种常见的方式是通过`Builder`的扩展方法,如创建一个新的`Builder`子类并添加新的构建方法。 在性能优化方面,`AlertDialog`会复用对话框实例,避免频繁创建和销毁。当对话框关闭时,它并不会立即销毁,而是...

    智能家居系统 普通对话框Dialog-教案.doc

    为了深化理解,教学过程中还可以扩展到其他类型的对话框,如下拉列表对话框、单选按钮对话框和复选按钮对话框,这些可以通过`AlertDialog.Builder`的`setSingleChoiceItems()`或`setMultiChoiceItems()`方法实现。...

    AlertDialogDemo

    `AlertDialogDemo`通常是一个示例项目,用于演示如何在应用程序中有效地使用`AlertDialog`。`AlertDialog`有两种基本模式:单按钮对话框和多按钮对话框。下面我们将详细探讨这两种模式以及如何在Android应用中实现...

    android 各种对话框

    在Java代码中,可以通过`AlertDialog.Builder`类创建一个消息框。例如: ```java new AlertDialog.Builder(self) .setTitle("标题") .setMessage("简单消息框") .setPositiveButton("确定", null) .show(); ```...

    android开发中常用的5中对话框

    开发者可以使用`AlertDialog.Builder`类来创建此类对话框。例如: ```java new AlertDialog.Builder(context) .setTitle("提示") .setMessage("这是一个普通对话框") .setPositiveButton("确定", new ...

    android messagebox

    在给定的代码中,通过`new AlertDialog.Builder(this)`创建了一个AlertDialog的Builder对象,然后调用`setMessage("AAA")`设置对话框的消息文本为“AAA”,最后通过`create()`方法创建对话框实例,并调用`show()`...

    各种dialog

    以上只是Dialog的基本使用方法,实际应用中还可以根据需求进行更多的自定义和扩展。DialogDemo项目很可能会包含这些示例,通过阅读和理解代码,开发者可以更好地掌握如何在Android应用中灵活运用Dialog。

    自定义AlertDialog_android_v1.0.01

    具体来说,更新允许开发者传入自定义的View资源来构建对话框的内容和外观,这极大地扩展了`AlertDialog`的可塑性。 自定义`AlertDialog`的过程主要包括以下几个步骤: 1. **创建布局资源**:首先,你需要在项目的`...

    anroid HerilyAlertDialog完全自定义的Dialog Demo.zip

    在Android开发中,`AlertDialog`是系统提供的一种用于与用户交互的对话框组件,它能够显示重要的信息或者请求用户的...通过查看这些文件,开发者可以学习到如何根据具体需求来调整和扩展Android的`AlertDialog`功能。

    DialogFagment支付弹出框

    在Android开发中,`DialogFragment` 是一个非常重要的组件,它允许我们以对话框的形式展示内容,而`DialogFragment`的圆角弹出框则是一种常见的美化方式,能提升应用的用户体验。本篇将深入讲解如何创建并自定义具有...

Global site tag (gtag.js) - Google Analytics