主文件

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. 简单易用的MongoDB

    从我第一次听到Nosql这个概念到如今已经走过4个年头了,但仍然没有具体的去做过相应的实践.最近获得一段学习休息时间,购买了Nosql技术实践一书,正在慢慢的学习.在主流观点中,Nosql大体分为4类 ...

  2. Win8Metro(C#)数字图像处理--2.18图像平移变换

    原文:Win8Metro(C#)数字图像处理--2.18图像平移变换  [函数名称] 图像平移变换函数TranslationProcess(WriteableBitmap src,int x,in ...

  3. 蚂蚁金服招聘-JAVA资深开发工程师/专家-蚂蚁金服保险

    岗位描述: 1.协助业务方梳理业务需求,提供业务规划方案.架构设计方案: 2.负责业务系统的规划设计,制定产品的技术发展路线,完成重要业务模块及核心框架的搭建及编码实现: 3.发现和解决业务系统的技术 ...

  4. .NET环境下有关打印页面设置、打印机设置、打印预览对话框的实现

    原文:.NET环境下有关打印页面设置.打印机设置.打印预览对话框的实现 我个人认为,开发MIS,首先就得解决网格的问题,而开发工具为我们提供了如DataGrid.MSHFlexGrid的控件.其次,是 ...

  5. SQL Server中 SET 和 SELECT 赋值有什么区别?

    SQL Server 中对已经定义的变量赋值的方式用两种,分别是 SET 和 SELECT.对于这两种方式的区别,SQL Server 联机丛书中已经有详细的说明,但很多时候我们并没有注意,其实这两种 ...

  6. ext中设置grid单选,取消选择

    原代码(无法取消选择)如下: selModel: Ext.create('Ext.selection.CheckboxModel', { mode: "SINGLE", allow ...

  7. redis连接错误3种解决方案System Error MISCONF Redis is configured to save RDB snapshots

    redis连接错误System Error MISCONF Redis is configured to save RDB snapshots, but XX   情况1解决办法: 由于强制停止red ...

  8. AcWing 164. 可达性统计

    给定一张N个点M条边的有向无环图,分别统计从每个点出发能够到达的点的数量. 输入格式 第一行两个整数N,M,接下来M行每行两个整数x,y,表示从x到y的一条有向边. 输出格式 输出共N行,表示每个点能 ...

  9. python算法与数据结构-栈(43)

    一.栈的介绍 栈作为一种数据结构,是一种只能在一端进行插入和删除操作.它按照先进后出的原则存储数据,先进入的数据被压入栈底,最后的数据在栈顶,需要读数据的时候从栈顶开始弹出数据(最后一个数据被第一个读 ...

  10. 【转载】JDK自带的log工具

    版权声明:本文为Jaiky_杰哥原创,转载请注明出处.This blog is written by Jaiky, reproduced please indicate. https://blog.c ...