主文件

  1. package cn.com.sxp;

    import android.app.Activity;
    import android.app.AlertDialog;
    import android.content.DialogInterface;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.Toast;

    public class AlertDialogActivity extends Activity implements OnClickListener {
    private Button btnOne = null;
    private Button btnTwo = null;
    private Button btnThree = null;
    private AlertDialog.Builder builder = null;
    private AlertDialog alert = null;
    final CharSequence[] items = {"红", "绿", "蓝"};

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    btnOne = (Button) findViewById(R.id.btnOne);
    btnTwo = (Button) findViewById(R.id.btnTwo);
    btnThree = (Button) findViewById(R.id.btnThree);

    btnOne.setOnClickListener(this);
    btnTwo.setOnClickListener(this);
    btnThree.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
    switch (v.getId()) {
    case R.id.btnOne:
    // Builder构造函数
    // Constructor using a context for this builder and the AlertDialog it creates.
    builder = new AlertDialog.Builder(this);
    // setMessage:
    // Set the message to display.
    builder.setMessage("我乃石大虾")
    // setCancelable
    // Sets whether the dialog is cancelable or not. Default is true.
    .setCancelable(false)
    // setPositiveButton
    // Set a listener to be invoked when the positive button of the dialog is pressed.
    .setPositiveButton("是", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
    AlertDialogActivity.this.finish();
    }
    })
    .setNegativeButton("不", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
    dialog.cancel();
    }
    });
    alert = builder.create();
    alert.show();
    break;
    case R.id.btnTwo:
    builder = new AlertDialog.Builder(this);
    builder.setTitle("请选择一种颜色");
    // setItems
    // Set a list of items to be displayed in the dialog as the content, you will be notified of the selected item via the supplied listener.
    builder.setItems(items, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int item) {
    Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
    }
    });
    alert = builder.create();
    alert.show();
    break;
    case R.id.btnThree:
    builder = new AlertDialog.Builder(this);
    builder.setTitle("请选择一种颜色,与上面不一样");
    // setSingleChoiceItems
    // Set a list of items to be displayed in the dialog as the content, you will be notified of the selected item via the supplied listener. The list
    // will have a check mark displayed to the right of the text for the checked item. Clicking on an item in the list will not dismiss the dialog.
    // Clicking on a button will dismiss the dialog.
    // -1表示没有相被选择
    // builder.setMultiChoiceItems(items, -1, listener)
    builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int item) {
    Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
    }
    });
    alert = builder.create();
    alert.show();
    default:

    }
    }
    }

XML文件

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

    <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello" />

    <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/btnOne"
    android:text="一般" />

    <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/btnTwo"
    android:text="列表" />

    <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/btnThree"
    android:text="可选" />

    </LinearLayout>

运行效果:

点击一;

点击二;

点击三

android_alertDialog的更多相关文章

  1. Android相关sdk使用

      SimpleDateFormat使用详解 Android_AlertDialog 两分钟彻底让你明白Android Activity生命周期(图文)! Android布局控件之LinearLayo ...

  2. android 基本控件使用

    http://tech.it168.com/a2012/0321/1327/000001327704.shtml Android_ListView_用代码控制ListView的位置 有三种方法 mli ...

随机推荐

  1. QDialog在hide()之后,就被销毁的原因

    一路跟踪源码,关键就是这两句: int QDialog::exec() { Q_D(QDialog); if (d->eventLoop) { qWarning("QDialog::e ...

  2. 高手问答精选:Go 语言 —— 云计算时代的 C 语言(类似于一个FAQ)

    Go 语言被称为云计算时代的 C 语言,它在软件开发效率和运行效率之间做出了绝佳的权衡.这使得它既适应于互联网应用的极速开发,又能在高并发.高性能的开发场景中如鱼得水.正因如此,许多互联网公司,尤其是 ...

  3. Win10《芒果TV - Preview》更新v3.1.31.0,全新播放页蜕变,预加载提速技术

    Win10<芒果TV - Preview>(商店内测版) v3.1.31.0 于2016年11月21日星期一晚上九点半登陆商店 主要是全面升级改造桌面播放页,新增观看互动评论.猜你喜欢功能 ...

  4. error C2760: 语法错误: 意外的令牌“标识符”,预期的令牌为“类型说明符”

    解决办法: 打开项目工程----> 属性 ---> c/c++ --> 语音 --> 符合模式 修改成否即可

  5. 给文件右击菜单增加7-ZIP浏览功能(用注册表设置Shell调用预览命令)

    疯狂delphi delphiXE7.XE8.XE10公开课A 群号:58592705 QQ:513187410 朱建强 BAT-给文件右击菜单增加7-ZIP浏览功能 Reg给文件右击菜单增加7-ZI ...

  6. AlwaysOn数据同步暂停及回退技术

    随着AlwaysOn技术的流行,关于AlwayOn的问题也越来越多,某企业搭建有三副本的AlwaysOn一套,现想修改主节点上某张表的某个数据,看看会出现什么后果,如果结果正常,就同步到其他节点上:如 ...

  7. 数据备份服务商Rubrik获4000万美元B轮融资

    搜狐科技 文/丽丽卡 5月27日,数据备份服务商Rubrik获Greylock Partners领投的4000万美元B轮融资,Lightspeed Venture Partners及其现有投资者跟投, ...

  8. MSB3268 .Net 4.0工程 引用BCL错误

    Severity Code Description Project File Line Suppression StateWarning MSB3268 The primary reference & ...

  9. php中$_REQUEST、$_POST、$_GET的区别

    php中$_REQUEST.$_POST.$_GET的区别1. $_REQUEST php中$_REQUEST可以获取以POST方法和GET方法提交的数据,缺点:速度比较慢 . 2. $_GET 用来 ...

  10. impala学习笔记

    impala学习笔记 -- 建库 CREATE DATABASE IF NOT EXISTS database_name; -- 在HDFS文件系统中创建数据库,需要指定要创建数据库的位置. CREA ...