创建项目AlertDialogDemo如图:

AlertDialogDemo.java文件代码如下:
package
zerone.AlertDialogDemo;
import
android.app.Activity;
public
class
AlertDialogDemo
extends
Activity {
/** Called when the activity is first created. */
final
int
DIALOG_WELCOME =
1
;
private
Button btn_alert;
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn_alert=(Button)findViewById(R.id.btn_dialog);
btn_alert.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View v) {
showDialog(DIALOG_WELCOME);
}
});
}
@Override
protected
Dialog onCreateDialog(
int
id, Bundle args) {
switch
(id) {
case
DIALOG_WELCOME:
return
new
AlertDialog.Builder(AlertDialogDemo.
this
)
.setTitle(
"欢迎"
).setMessage(
"欢迎使用本程序"
)
.setIcon(android.R.drawable.ic_dialog_info)
.setPositiveButton(
"确定"
,
new
OnClickListener() {
@Override
public
void
onClick(DialogInterface dialog,
int
which) {
Toast.makeText(AlertDialogDemo.
this
,
"点击\"确定\"按钮后"
, Toast.LENGTH_SHORT).show();
}
}).create();
default
:
return
null
;
}
}
}
main.xml代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" >
<TextView
android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/app_title" />
<Button
android:id="@+id/btn_dialog"
android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="弹出"/> </LinearLayout>




- 大小: 50.4 KB

- 大小: 15.1 KB

- 大小: 19.6 KB

- 大小: 27.9 KB
分享到:
相关推荐
【Android AlertDialog简单使用实例】 在Android开发中,`AlertDialog`是一个非常重要的组件,它用于向用户展示信息或者需要用户确认的交互场景。本篇将详细解释如何在Android应用中使用`AlertDialog`,并提供两种...
5. **构建并显示对话框**:最后,使用`create()`方法创建`AlertDialog`实例,并调用`show()`来显示对话框。 ```java AlertDialog dialog = builder.create(); dialog.show(); ``` 在压缩包文件`...
1. **创建对话框对象**:首先,你需要创建一个`AlertDialog.Builder`实例,这是构建对话框的基础。你可以通过`new AlertDialog.Builder(Context)`来初始化。 ```java AlertDialog.Builder builder = new ...
在这个实例中,我们将会深入探讨如何创建和使用`AlertDialog`。 首先,要创建一个`AlertDialog`,你需要先有一个`Context`对象,例如Activity或者Application。`AlertDialog`的构建通常分为以下几个步骤: 1. **...
本教程将详细介绍如何创建和使用`AlertDialog`,适合Android开发初学者。 ### 1. AlertDialog的基本结构 `AlertDialog`由三部分组成:标题(title)、消息(message)和按钮(buttons)。标题可以用来简要说明...
`AlertDialog`是Android SDK中的`android.app.AlertDialog`类的实例,提供了构建具有自定义外观和行为的对话框的功能。 1. **创建AlertDialog** 创建`AlertDialog`通常涉及以下步骤: - 创建`AlertDialog.Builder...
最后,通过`create()`方法创建`AlertDialog`实例,并调用`show()`来显示对话框。 示例代码: ```java AlertDialog.Builder builder = new AlertDialog.Builder(Main.this); builder.setMessage("确认退出吗?")...
最后,通过`create()`方法创建`AlertDialog`实例,并调用`show()`方法将其显示出来。 ```java AlertDialog dialog = builder.create(); dialog.show(); ``` 五、`CustomListDialog`示例 在`CustomListDialog`这个...
通过`create()`方法创建`AlertDialog`实例,并使用`show()`方法将其显示出来。 ```java AlertDialog dialog = builder.create(); dialog.show(); ``` 二、`AlertDialog`的自定义 1. 自定义样式:可以通过...
在本文中,我们将深入探讨 `AlertDialog` 的基本用法,并通过实例代码来展示其功能实现。 首先,`AlertDialog` 是 `Dialog` 类的一个子类,它是Android系统提供的标准对话框。创建 `AlertDialog` 需要通过 `...
Android 自定义实现一个AlertDialog对话框,提示框或警告框弹出式对话框,套用了系统Holo风格,生成符合系统主题的AlertDialog.Builder,可以分别定义弹出提示的标题文字及提示内容: builder.setTitle("Test ...
本文将深入探讨`AlertDialog`的源码,理解其内部工作原理以及如何自定义和使用。 首先,`AlertDialog`是`Dialog`类的一个子类,它通过`AlertDialog.Builder`来创建实例。`Builder`类提供了丰富的接口用于设置对话框...
在Android Studio中,我们可以使用`AlertDialog.Builder`来构建一个`AlertDialog`实例。以下是一个简单的创建`AlertDialog`的基本步骤: 1. 首先,你需要引入必要的库: ```java import android.app.AlertDialog; ...
- 实例化`AlertDialog.Builder`:这是构建`AlertDialog`的对象,可以通过传递上下文(Context)来初始化。 - 设置属性:如标题、消息、按钮等,使用`setTitle()`、`setMessage()`、`setPositiveButton()`、`...
3. 创建并显示`AlertDialog`:调用`Builder`的`create()`方法生成`AlertDialog`实例,然后调用`show()`方法将其显示出来。 ```java AlertDialog dialog = builder.create(); dialog.show(); ``` 4. 自定义布局:...
通过这个项目,学习者可以实践创建一个简单的UI,包括显示Toast提示、创建菜单、使用ListView展示数据以及弹出AlertDialog进行用户交互。这五个组件是Android开发中的基础,理解并熟练掌握它们对于构建功能丰富的...