Android AlertDialog 实例

本文介绍如何在Android应用中使用AlertDialog实现离开程序前的确认提示功能,包括设置标题、图标、消息框和确认/取消按钮。

 1 package com.turboradio.googlesdk;
2
3 import android.app.Activity;
4 import android.app.AlertDialog;
5 import android.content.DialogInterface;
6 import android.os.Bundle;
7 import android.view.View;
8
9 public class Ex_4_26 extends Activity {
10
11 @Override
12 protected void onCreate(Bundle savedInstanceState) {
13 super.onCreate(savedInstanceState);
14 setContentView(R.layout.ex_4_26);
15 }
16 /**离开程序**/
17 public void leave(View v){
18 new AlertDialog.Builder(Ex_4_26.this)
19 /**设置标题**/
20 .setTitle("重要")
21 /**设置icon**/
22 .setIcon(android.R.drawable.alert_dark_frame)
23 /**设置内容**/
24 .setMessage("你确定要关闭吗?")
25 .setNegativeButton("取消", new DialogInterface.OnClickListener(){
26
27 public void onClick(DialogInterface dialog, int which) {
28 // TODO Auto-generated method stub
29
30 }}).setPositiveButton("确定", new DialogInterface.OnClickListener(){
31
32 public void onClick(DialogInterface dialog, int which) {
33 /**关闭窗口**/
34 finish();
35
36 }}).show();
37
38 }
39 }

ex_4_26.xml

 1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="fill_parent"
4 android:layout_height="fill_parent"
5 android:orientation="vertical" >
6 <Button
7 android:layout_width="wrap_content"
8 android:layout_height="wrap_content"
9 android:text="按我离开"
10 android:onClick="leave"
11 />
12 </LinearLayout>



转载于:https://www.cnblogs.com/jiayonghua/archive/2011/12/05/2276962.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值