主文件

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. apache本地服务器的配置流程

    安装Apache 一.目的: 1. 能够有一个测试的服务器,不是所有的特殊网络服务都能找到免费的! 二.为什么是 "Apache" 1. 使用最广的 Web 服务器 2. Mac自 ...

  2. delphi 程序强制结束自身(两种方法都暴力)

    procedure KillSelf;begin  Sleep(1000);  if not TerminateProcess(GetCurrentProcessId, 0) then  WinExe ...

  3. MIPS开发板的“不二”选择——Creator Ci20单板计算机评测(芯片是君正JZ4780 ,也就是MIPS R3000,系统推荐Debian或深度,官网就有,其它语言有FreePascal和Go和Java和Python)

    在MIPS架构的CPU上开发软件,当然需要使用MIPS专用的工具链来编译代码.不过一般的LINUX发行版内都有相应的配套工具链供用户使用.Ci20出厂时的LINUX发行版为DEBIAN 7.5,相应的 ...

  4. Qt中连接到同一signal的多个slots的执行顺序问题(现在是看连接顺序,以前是无序的)

    in the order they have been connected 起源 前些天忘记在哪儿讨论过这个问题,今天在csdn又看到有网友问这个问题,而其他网友却无一例外的给出了“无序”这个答案. ...

  5. python中的基本数据类型之字典

    一.字典的简单介绍 字典是(dict)是python中惟一的一个映射类型,他是以{}括起来的键值对组成,在字典中key是唯一的,在保存的时候,根据key来计算出一个内存地址,然后将key-value保 ...

  6. spring常见十大异常

    一.找不到配置文件的异常 [plain] view plaincopy org.springframework.beans.factory.BeanDefinitionStoreException:  ...

  7. KNN算法——分类部分

    1.核心思想 如果一个样本在特征空间中的k个最相邻的样本中的大多数属于某一个类别,则该样本也属于这个类别,并具有这个类别上样本的特性.也就是说找出一个样本的k个最近邻居,将这些邻居的属性的平均值赋给该 ...

  8. Tomcat常见问题[内存溢出,虚拟目录配置等](一)

    一.Tomcat内存溢出的解决方法 内存溢出一般有如下三种常见的原因: OutOfMemoryError: Java heap space OutOfMemoryError: PermGen spac ...

  9. PWN菜鸡入门之栈溢出(1)

    栈溢出 一.基本概念: 函数调用栈情况见链接 基本准备: bss段可执行检测: ​ gef➤ b main Breakpoint at . gef➤ r Starting program: /mnt/ ...

  10. memcached--delete--replace--set--get--incr--decr--stats

    memcached命令 1.get  key 来获取在内存中的值 get name 2.delete  key 删除在内存中的值 delete name 3.replace  key flag exp ...