1. MainActivity.java  
  2.     
  3. public class MainActivity extends Activity {  
  4.     TextView show;  
  5.     String[] items = new String[] {  
  6.             "疯狂Java讲义", "疯狂Ajax讲义",  
  7.             "轻量级Java EE企业应用实战",  
  8.             "疯狂Android讲义" };  
  9.     
  10.     @Override  
  11.     protected void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.         setContentView(R.layout.main);  
  14.         show = (TextView) findViewById(R.id.show);  
  15.     }  
  16.     
  17.     public void simple(View source)  
  18.     {  
  19.         AlertDialog.Builder builder = new AlertDialog.Builder(this)  
  20.                 // 设置对话框标题  
  21.                 .setTitle("简单对话框")  
  22.                 // 设置图标  
  23.                 .setIcon(R.drawable.tools)  
  24.                 .setMessage("对话框的测试内容\n第二行内容");  
  25.         // 为AlertDialog.Builder添加"确定"按钮  
  26.         setPositiveButton(builder);  
  27.         // 为AlertDialog.Builder添加"取消"按钮  
  28.         setNegativeButton(builder)  
  29.                 .create()  
  30.                 .show();  
  31.     }  
  32.     
  33.     public void simpleList(View source)  
  34.     {  
  35.         AlertDialog.Builder builder = new AlertDialog.Builder(this)  
  36.                 // 设置对话框标题  
  37.                 .setTitle("简单列表对话框")  
  38.                 // 设置图标  
  39.                 .setIcon(R.drawable.tools)  
  40.                 // 设置简单的列表项内容  
  41.                 .setItems(items, new OnClickListener()  
  42.                 {  
  43.                     @Override  
  44.                     public void onClick(DialogInterface dialog, int which)  
  45.                     {  
  46.                         show.setText("你选中了《" + items[which] + "》");  
  47.                     }  
  48.                 });  
  49.         // 为AlertDialog.Builder添加"确定"按钮  
  50.         setPositiveButton(builder);  
  51.         // 为AlertDialog.Builder添加"取消"按钮  
  52.         setNegativeButton(builder)  
  53.                 .create()  
  54.                 .show();  
  55.     }  
  56.     
  57.     public void singleChoice(View source)  
  58.     {  
  59.         AlertDialog.Builder builder = new AlertDialog.Builder(this)  
  60.                 // 设置对话框标题  
  61.                 .setTitle("单选列表项对话框")  
  62.                 // 设置图标  
  63.                 .setIcon(R.drawable.tools)  
  64. )  
  65.                 .setSingleChoiceItems(items, 1, new OnClickListener()  
  66.                 {  
  67.                     @Override  
  68.                     public void onClick(DialogInterface dialog, int which)  
  69.                     {  
  70.                         show.setText("你选中了《" + items[which] + "》");  
  71.                     }  
  72.                 });  
  73.         // 为AlertDialog.Builder添加"确定"按钮  
  74.         setPositiveButton(builder);  
  75.         // 为AlertDialog.Builder添加"取消"按钮  
  76.         setNegativeButton(builder)  
  77.                 .create()  
  78.                 .show();  
  79.     }  
  80.     
  81.     public void multiChoice(View source)  
  82.     {  
  83.         AlertDialog.Builder builder = new AlertDialog.Builder(this)  
  84.                 // 设置对话框标题  
  85.                 .setTitle("多选列表项对话框")  
  86.                 // 设置图标  
  87.                 .setIcon(R.drawable.tools)  
  88. 项、第4项  
  89.                 .setMultiChoiceItems(items  
  90.                         , new boolean[]{false , true ,false ,true}, null);  
  91.         // 为AlertDialog.Builder添加"确定"按钮  
  92.         setPositiveButton(builder);  
  93.         // 为AlertDialog.Builder添加"取消"按钮  
  94.         setNegativeButton(builder)  
  95.                 .create()  
  96.                 .show();  
  97.     }  
  98.     
  99.     public void customList(View source)  
  100.     {  
  101.         AlertDialog.Builder builder = new AlertDialog.Builder(this)  
  102.                 // 设置对话框标题  
  103.                 .setTitle("自定义列表项对话框")  
  104.                 // 设置图标  
  105.                 .setIcon(R.drawable.tools)  
  106.                 // 设置自定义列表项  
  107.                 .setAdapter(new ArrayAdapter<String>(this  
  108.                         , R.layout.array_item  
  109.                         , items), null);  
  110.         // 为AlertDialog.Builder添加"确定"按钮  
  111.         setPositiveButton(builder);  
  112.         // 为AlertDialog.Builder添加"取消"按钮  
  113.         setNegativeButton(builder)  
  114.                 .create()  
  115.                 .show();  
  116.     }  
  117.     
  118.     public void customView(View source)  
  119.     {  
  120.         // 装载app\src\main\res\layout\login.xml界面布局文件  
  121.         TableLayout loginForm = (TableLayout)getLayoutInflater()  
  122.                 .inflate( R.layout.login, null);  
  123.         new AlertDialog.Builder(this)  
  124.                 // 设置对话框的图标  
  125.                 .setIcon(R.drawable.tools)  
  126.                 // 设置对话框的标题  
  127.                 .setTitle("自定义View对话框")  
  128.                 // 设置对话框显示的View对象  
  129.                 .setView(loginForm)  
  130.                 // 为对话框设置一个"确定"按钮  
  131.                 .setPositiveButton("登录", new OnClickListener() {  
  132.                     @Override  
  133.                     public void onClick(DialogInterface dialog,  
  134.                                         int which) {  
  135.                         // 此处可执行登录处理  
  136.                     }  
  137.                 })  
  138.                 // 为对话框设置一个"取消"按钮  
  139.                 .setNegativeButton("取消", new OnClickListener()  
  140.                 {  
  141.                     @Override  
  142.                     public void onClick(DialogInterface dialog,  
  143.                                         int which)  
  144.                     {  
  145.                         // 取消登录,不做任何事情  
  146.                     }  
  147.                 })  
  148.                 // 创建并显示对话框  
  149.                 .create()  
  150.                 .show();  
  151.     }  
  152.     
  153.     
  154.     private AlertDialog.Builder setPositiveButton(  
  155.             AlertDialog.Builder builder)  
  156.     {  
  157.         // 调用setPositiveButton方法添加"确定"按钮  
  158.         return builder.setPositiveButton("确定", new OnClickListener()  
  159.         {  
  160.             @Override  
  161.             public void onClick(DialogInterface dialog, int which)  
  162.             {  
  163.                 show.setText("单击了【确定】按钮!");  
  164.             }  
  165.         });  
  166.     }  
  167.     private AlertDialog.Builder setNegativeButton(  
  168.             AlertDialog.Builder builder)  
  169.     {  
  170.         // 调用setNegativeButton方法添加"取消"按钮  
  171.         return builder.setNegativeButton("取消", new OnClickListener()  
  172.         {  
  173.             @Override  
  174.             public void onClick(DialogInterface dialog, int which)  
  175.             {  
  176.                 show.setText("单击了【取消】按钮!");  
  177.             }  
  178.         });  
  179.     }  
  180. }  
  181.     
  182.     
  183. XML文件  
  184.     
  185. Simple-item.xml  
  186.     
  187. <?xml version="1.0" encoding="utf-8"?>  
  188. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  189.     android:orientation="horizontal"  
  190.     android:layout_width="match_parent"  
  191.     android:layout_height="wrap_content">  
  192. <!-- 定义一个ImageView,用于作为列表项的一部分。 -->  
  193. <ImageView android:id="@+id/header"  
  194.     android:layout_width="wrap_content"  
  195.     android:layout_height="wrap_content"   
  196.     android:paddingLeft="10dp"/>  
  197. <LinearLayout  
  198.     android:orientation="vertical"  
  199.     android:layout_width="match_parent"  
  200.     android:layout_height="wrap_content">  
  201. <!-- 定义一个TextView,用于作为列表项的一部分。 -->  
  202. <TextView android:id="@+id/name"  
  203.     android:layout_width="wrap_content"   
  204.     android:layout_height="wrap_content"  
  205.     android:textSize="20dp"  
  206.     android:textColor="#f0f"  
  207.     android:paddingLeft="10dp"/>  
  208. <!-- 定义一个TextView,用于作为列表项的一部分。 -->  
  209. <TextView android:id="@+id/desc"  
  210.     android:layout_width="wrap_content"   
  211.     android:layout_height="wrap_content"  
  212.     android:textSize="14dp"  
  213.     android:paddingLeft="10dp"/>  
  214. </LinearLayout>  
  215. </LinearLayout>  
  216.     
  217.     
  218. Main.xml  
  219.     
  220. <?xml version="1.0" encoding="utf-8"?>  
  221. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  222.     android:orientation="vertical"  
  223.     android:layout_width="match_parent"  
  224.     android:layout_height="match_parent"  
  225.     android:paddingTop="40dp"  
  226.     android:gravity="center_horizontal">  
  227. <!-- 显示一个普通的文本编辑框组件 -->  
  228. <EditText   
  229.     android:id="@+id/show"  
  230.     android:layout_width="match_parent"   
  231.     android:layout_height="wrap_content"   
  232.     android:editable="false"/>  
  233. <!-- 定义一个普通的按钮组件 -->  
  234. <Button  
  235.     android:layout_width="match_parent"   
  236.     android:layout_height="wrap_content"   
  237.     android:text="简单对话框"  
  238.     android:onClick="simple"  
  239.     />  
  240. <!-- 定义一个普通的按钮组件 -->  
  241. <Button  
  242.     android:layout_width="match_parent"   
  243.     android:layout_height="wrap_content"   
  244.     android:text="简单列表项对话框"  
  245.     android:onClick="simpleList"  
  246.     />     
  247. <!-- 定义一个普通的按钮组件 -->  
  248. <Button  
  249.     android:layout_width="match_parent"   
  250.     android:layout_height="wrap_content"   
  251.     android:text="单选列表项对话框"  
  252.     android:onClick="singleChoice"  
  253.     />     
  254. <!-- 定义一个普通的按钮组件 -->  
  255. <Button  
  256.     android:layout_width="match_parent"   
  257.     android:layout_height="wrap_content"   
  258.     android:text="多选列表项对话框"  
  259.     android:onClick="multiChoice"  
  260.     />     
  261. <!-- 定义一个普通的按钮组件 -->  
  262. <Button  
  263.     android:layout_width="match_parent"   
  264.     android:layout_height="wrap_content"   
  265.     android:text="自定义列表项对话框"  
  266.     android:onClick="customList"  
  267.     />     
  268. <!-- 定义一个普通的按钮组件 -->  
  269. <Button  
  270.     android:layout_width="match_parent"   
  271.     android:layout_height="wrap_content"   
  272.     android:text="自定义View对话框"  
  273.     android:onClick="customView"  
  274.     />                     
  275. </LinearLayout>  
  276.     
  277. Login.xml  
  278.     
  279. <?xml version="1.0" encoding="utf-8"?>  
  280. <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  281.              android:id="@+id/loginForm"  
  282.              android:layout_width="match_parent"  
  283.              android:layout_height="match_parent">  
  284.     <TableRow>  
  285.         <TextView  
  286.             android:layout_width="match_parent"  
  287.             android:layout_height="wrap_content"  
  288.             android:text="用户名:"  
  289.             android:textSize="10pt"/>  
  290.         <!-- 输入用户名的文本框 -->  
  291.         <EditText  
  292.             android:layout_width="match_parent"  
  293.             android:layout_height="wrap_content"  
  294.             android:hint="请填写登录账号"  
  295.             android:selectAllOnFocus="true"/>  
  296.     </TableRow>  
  297.     <TableRow>  
  298.         <TextView  
  299.             android:layout_width="match_parent"  
  300.             android:layout_height="wrap_content"  
  301.             android:text="密码:"  
  302.             android:textSize="10pt"/>  
  303.         <!-- 输入密码的文本框 -->  
  304.         <EditText  
  305.             android:layout_width="match_parent"  
  306.             android:layout_height="wrap_content"  
  307.             android:hint="请填写密码"  
  308.             android:password="true"/>  
  309.     </TableRow>  
  310.     <TableRow>  
  311.         <TextView  
  312.             android:layout_width="match_parent"  
  313.             android:layout_height="wrap_content"  
  314.             android:text="电话号码:"  
  315.             android:textSize="10pt"/>  
  316.         <!-- 输入电话号码的文本框 -->  
  317.         <EditText  
  318.             android:layout_width="match_parent"  
  319.             android:layout_height="wrap_content"  
  320.             android:hint="请填写您的电话号码"  
  321.             android:selectAllOnFocus="true"  
  322.             android:phoneNumber="true"/>  
  323.     </TableRow>  
  324. </TableLayout>  

效果

AlertDialog设计对话框的更多相关文章

  1. Android 自定义AlertDialog退出对话框

    Android 自定义AlertDialog退出对话框 转 https://blog.csdn.net/wkh11/article/details/53081634在项目中很多时候会出现点击返回键出现 ...

  2. Android开发之使用AlertDialog创建对话框,单选框和多选框

    对话框: 对话框的icon,title,message等都可以不设置. 单选框和多选框与对话框勾选步骤基本上一致. 对话框的构建步骤: 1.使用AlertDialog类的内部类Builder类new ...

  3. AlertDialog.Builder对话框类的用法

    1.在测试时,如何实现一个提示 可以使用 Toast.makeText(this, "这是一个提示", Toast.LENGTH_SHORT).show(); //从资源文件str ...

  4. 使用AlertDialog创建对话框的大致步骤

    1.创建AlertDialog.Builder对象,该对象是AlertDialog的创建器.2.调用AlertDialog.Builder的方法为对话框设置图标.标题.内容等.3.调用AlertDia ...

  5. android 开发AlertDialog.builder对话框的实现

    AndroidAPI提供了Dialog对话框控件,但google明确指出不建议开发者只是使用Dialog来创建对话框,而应该自定义对话框或者使用API中提供的Dialog的子类,如AlertDialo ...

  6. AlertDialog创建对话框的测试

    AlertDialog的功能是非常强大的,它可以创建各种对话框,它的结构分为:图标区.标题区.内容区.按钮区共四个区域.以这样的思路区创建AlertDialog是非常简单的. 创建AlertDialo ...

  7. AlertDialog提示对话框练习

    闲来无事,就练习了下AlertDialog对话框. 首先写了三个button,分别打开一般对话框,单选列表对话框和复选对话框. xml代码 <LinearLayout xmlns:android ...

  8. Android学习笔记使用AlertDialog实现对话框

    使用AlertDialog可以实现如下对话框 案例 布局问文件就加了几个Button,我直接上Java代码了 实现显示带取消,确定按钮的对话框按钮 Button showDialogOne = fin ...

  9. Android设置AlertDialog点击按钮对话框不关闭(转)

    (转自:http://blog.csdn.net/winson_jason/article/details/8485524) 当我们在用到Android alertDialog创建对话框 的时候,我们 ...

随机推荐

  1. as3 代码优化

    1 代码写法 1 定义局部变量 定义局部变量的时候,一定要用关键字var来定义,因为在Flash播放器中,局部变量的运行速度更快,而且在他们的作用域外是不耗占系统资源的.当一个函数调用结束的时候,相应 ...

  2. ntohs, ntohl, htons,htonl的比较和详解【转】

    ntohs =net to host short int 16位 htons=host to net short int 16位 ntohs =net to host long int 32位 hto ...

  3. cvc-complex-type.2.3: Element 'beans' cannot have character [children]

    当启动spring的项目时,有时候会抛如下异常: Line 33 in XML document from ServletContext resource [/WEB-INF/backend-serv ...

  4. Packed with amazing data about the world in 201

    Only those who have the patience to do simple things,perfectly ever acquire the skill to do difficul ...

  5. python 学习备忘

    list列表排序 def listdir(path): #返回一个排序后的目录list files=os.listdir(path) files.sort(key=lambda x:str(x[:-4 ...

  6. JavaScipt测试调研

    JavaScript测试调研 前言 与其他语言相似,JavaScript的测试也会包括代码审查.单元测试等内容.本文就JavaScript的测试调研了一些测试工具和测试框架. 相对于其他很多高级语言语 ...

  7. 大型运输行业实战_day04_3_高级查询+分页

    1.高级查询+分页最终结果 2.分页的本质分析 前端传入:当前页  和  每页显示条数 数据库必须查询出:数据列表 和 总共条数 页面显示包括的数据有: 列表 +  每页显示条数 + 当前页 + 总共 ...

  8. PR数量回写重复

  9. Java List/HashSet/HashMap的排序

    在对Java无序类集合,如List(ArrayList/LinkedList).HashSet(TreeSet有序).HashMap等排序时,Java中一个公共的类Collections,提供了对Ja ...

  10. [udemy]WebDevelopment_How the Internet Works

    Browsing the web Enter google.com, who is this google.com This question gets asked all the way down ...