主文件

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文件

<?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. MFC应用程序配置不正确解决方案(manifest对依赖的强文件名,WinSxs是windows XP以上版本提供的非托管并行缓存)

    [现象] 对这个问题的研究是起源于这么一个现象:当你用VC++2005(或者其它.NET)写程序后,在自己的计算机上能毫无问题地运行,但是当把此exe文件拷贝到别人电脑上时,便不能运行了,大致的错误提 ...

  2. 没有安装提供程序“System.Data.SqlServerCe.3.5”的解决方法

    在Windows 8.1系统下运行带数据库功能的应用,报错并提示:“System.InvalidOperationException”类型的未经处理的异常在 System.Data.Linq.dll ...

  3. eclipse 插件编写(三)

    参考:http://help.eclipse.org/mars/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Fguide%2Fworkbench_ ...

  4. Android零基础入门第86节:探究Fragment生命周期

    一个Activity可以同时组合多个Fragment,一个Fragment也可被多个Activity 复用.Fragment可以响应自己的输入事件,并拥有自己的生命周期,但它们的生命周期直接被其所属的 ...

  5. IT安全军火库-转

    全球有260万信息安全专业人士,渗透测试工具是他们“安全军火库”中最常使用的装备,但直到最近,可用的渗透测试工具才丰富起来,但这也带来一个问题,挑选合适的渗透测试工具成了一件麻烦事,一个最简单的方法就 ...

  6. cloudsim 3.0.3下载与安装教程

    1.配置jdk(之前的文章都讲解过,这里就不具体说了) 2.安装eclipse或MyEclipse 3.下载cloudsim压缩包,这个的官网是需要FQ,这里贴上我的下载地址 链接:https://p ...

  7. Paragon NTFS for Mac 15.5.53 中文破解版(激活码)下载

    Paragon NTFS for Mac中文破解版是一款超级受欢迎的简单.高效.安全的格式读写软件,提供给大家,再也不用到处找Paragon NTFS序列号和Paragon NTFS激活码啦,帮您轻松 ...

  8. SpringBoot自定义注解@YamlPropertySource加载yml或者yaml文件(扩展了@PropertySource)

    1:概述 SpringBoot的@PropertySource注解只支持加载 properties结尾的文件.当使用@ConfigurationProperties 注解配合@EnableConfig ...

  9. 容器化之Docker小知识普及

    Docker确实是个好东西,相当于一个小型虚拟机,里面环境隔离只要有对应的镜像就能运行业务应用.运行基础应用.能跑数据库等等,还能跑linux等等. Docker 从狭义上来讲就是一个进程,从广义上来 ...

  10. vue复选框获取值的补充

    要通过vue的v-model获取选中复选框的值,可以用遍历对象的方式获取,代码如下: <!DOCTYPE html> <html xmlns="http://www.w3. ...