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 ...
随机推荐
- Windows 10开发基础——文件、文件夹和库(二)
主要内容: 使用选取器打开和保存文件 关于文件.文件夹和库,如果深究其实还是有比较多的内容,我们这一次来学习一下选取器就收了.还有上篇博文中读写文本文件的三种方式可以细细体会一下. 文件选取器包含文件 ...
- phpexcel导出超过26列解决方案
原文:phpexcel导出超过26列解决方案 将列的数字序号转成字母使用,代码如下: PHPExcel_Cell::stringFromColumnIndex($i); // 从o,1,2,3,.. ...
- 在Qt工程中加Boost
摘要: Boost是一个很强大的C++库,堪比STL,里面有很多非常优秀的类库.我不多介绍,详情见官网:http://www.boost.org/ 要在我们的Qt工程中把这个库加进去应该怎么做呢?我今 ...
- c# json key转大小写
有需求需要将json的字段转换为小写,使用正则表达式实现,代码如下 正则表达式为 \"[a-zA-Z0-9]+\"\s*: MatchCollection ms = Regex ...
- Ubuntu下使用Docker搭建MySQL步骤备忘
docker 安装和 pull MySQL镜像这里就不介绍了,很多介绍,建议去docker官方网站查看. 本文主要介绍MySQL container 运行起来之后的一些配置 在往下看之前,确保 doc ...
- Golang环境搭建,Notepad++配置Golang开发环境,Golang发送邮件
http://blog.csdn.net/u011012932/article/details/53102264 http://blog.csdn.net/u011012932/article/det ...
- Codility----PermMissingElem
Task description A zero-indexed array A consisting of N different integers is given. The array conta ...
- asp.net mvc下实现微信公众号(JsApi)支付介绍
本文主要讲解asp.net mvc框架下公众号支付如何实现,公众号支付主要包括三个核心代码,前台调起支付js代码.对应js调用参数参数生成代码.支付成功处理代码. 一.微信支付方式介绍 微信提供了各种 ...
- flask(四)
1.Flask-Session from flask import session,Flask from flask_session import Session #导入 from redis imp ...
- python常用数据结构(2)
1.有名字的元组——namedtuple >>> from collections import namedtuple >>> Point = namedtuple ...