WenJieWangFlyToWorld

导航

AlertDialog设计对话框

  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.                 // 设置单选列表项,默认选中第二项(索引为1  
  65.                 .setSingleChoiceItems(items, 1new 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.                 // 设置多选列表项,设置勾选第2项、第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>  

 

效果

 

posted on 2017-06-01 20:08  WenjieWangFlyToWorld  阅读(389)  评论(0编辑  收藏  举报