android_alertDialog
主文件
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的更多相关文章
- Android相关sdk使用
SimpleDateFormat使用详解 Android_AlertDialog 两分钟彻底让你明白Android Activity生命周期(图文)! Android布局控件之LinearLayo ...
- android 基本控件使用
http://tech.it168.com/a2012/0321/1327/000001327704.shtml Android_ListView_用代码控制ListView的位置 有三种方法 mli ...
随机推荐
- Win8 Metro(C#)数字图像处理--2.66FloodFill算法
原文:Win8 Metro(C#)数字图像处理--2.66FloodFill算法 [函数名称] 洪水填充算法函数 WriteableBitmap FloodfillProcess(Write ...
- SQLServer 服务器架构迁移
原文:SQLServer 服务器架构迁移 最近服务器架构迁移,将原来的服务器架构迁移到新的服务器,新的服务器在硬件方面比之前更好!原来服务器使用双向同步,并且为水平划分到多个数据库服务器.迁移过程中, ...
- 微信小程序把玩(九)scroll-view组件
原文:微信小程序把玩(九)scroll-view组件 scroll-view为滚动视图,分为水平滚动和垂直滚动.注意滚动视图垂直滚动时一定要设置高度否则的话scroll-view不会生效.滚动视图常用 ...
- Oracle 存储过程创建及调用
--------创建存储过程------- create or replace procedure TestSPas begin update table_name set CREATE_TIMEST ...
- ORA-02085: database link string connect to string
ORA-02085: database link string connects to string Cause: a database link connected to a database wi ...
- jdbc连接oracle数据库字符串
jdbc连接oracle数据库有两种方式: 连接数据库SID 连接数据库service_name 当连接SID时,字符串如下: url::orcl1" password="xxxx ...
- 关于联合体union的详细解释
1.概述 联合体union的定义方式与结构体一样,但是二者有根本区别. 在结构中各成员有各自的内存空间,一个结构变量的总长度是各成员长度之和.而在“联合”中,各成员共享一段内存空间,一个联合变量的长度 ...
- Windows下libevent C++封装类实现
题记 windows平台下对于服务器高并发的网络模型选型中,使用libevent是个不错的选择. 本文的背景基于:国内博客对于libevent大多介绍linux实现,大多是c语言的实现,Windows ...
- 制作Qt应用程序的插件(使用QtPlugin),对比DLL它是全平台通用的
在Qt下,插件有两种形式,一种是用于QtCreator下,扩展IDE功能.另一种是用于扩展开发者的应用.本文要讲的是后者. 定义一个纯虚类作为插件接口 #include <QtPlugin> ...
- javascript (java)动态时钟
<script language="javascript"> var t = null; t = setTimeout(time,1000);//开始执行 functi ...