码迷,mamicode.com
首页 > 其他好文 > 详细

020_01UI组件之Dialog详细用法

时间:2015-06-01 22:04:36      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:

 

先上图:

技术分享

点击“对话框1”,弹出如下对话框:

技术分享

点击“对话框2”,弹出如下对话框:

技术分享

点击“单选对话框”,弹出如下对话框:

技术分享

点击“多选对话框”,弹出如下对话框:

技术分享

点击“等待对话框”,弹出如下对话框:

技术分享

点击“进度对话框”,弹出如下对话框:

技术分享

 

源代码如下:

  1 package com.example.day20_01dialogdemo;
  2 
  3 import android.app.Activity;
  4 import android.app.AlertDialog.Builder;
  5 import android.app.ProgressDialog;
  6 import android.content.DialogInterface;
  7 import android.content.DialogInterface.OnClickListener;
  8 import android.content.DialogInterface.OnMultiChoiceClickListener;
  9 import android.content.Intent;
 10 import android.net.Uri;
 11 import android.os.Bundle;
 12 import android.os.SystemClock;
 13 import android.view.View;
 14 import android.widget.Toast;
 15 
 16 public class MainActivity extends Activity {
 17 
 18     @Override
 19     protected void onCreate(Bundle savedInstanceState) {
 20         super.onCreate(savedInstanceState);
 21         setContentView(R.layout.activity_main);
 22     }
 23 
 24     public void showDialog1(View v){
 25             
 26         //Dialog实现1:
 27 /*       Builder builder =  new Builder(this) ;
 28        builder.setTitle("Android 培训");
 29        builder.setMessage("是否浏览王道论坛?");
 30        builder.setPositiveButton("确认", new OnClickListener() {    
 31         @Override
 32         public void onClick(DialogInterface dialog, int which) {
 33             // TODO Auto-generated method stub
 34             Intent intent = new Intent();
 35             intent.setAction(Intent.ACTION_VIEW);
 36             Uri uri =Uri.parse("http://www.cskaoyan.com/");
 37             intent.setData(uri);
 38             //定义的intent里面么有规定type
 39             startActivity(intent);
 40             
 41             Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.cskaoyan.com/"));        
 42             startActivity(intent);
 43         }
 44         });
 45        builder.setNegativeButton("取消", null);
 46        builder.show();*/
 47        
 48        
 49        //Dialog实现2   
 50 /*       Builder builder =  new Builder(this) ;
 51        builder.setTitle("Android 培训");
 52        builder.setMessage("是否浏览王道论坛?");
 53        builder.setPositiveButton("确认", new OnClickListener() {    
 54         @Override
 55         public void onClick(DialogInterface dialog, int which) {
 56             // TODO Auto-generated method stub
 57             Intent intent = new Intent();
 58             intent.setAction(Intent.ACTION_VIEW);
 59             Uri uri =Uri.parse("http://www.cskaoyan.com/");
 60             intent.setData(uri);
 61             //定义的intent里面么有规定type
 62             startActivity(intent);
 63             
 64             Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.cskaoyan.com/"));        
 65             startActivity(intent);
 66         }
 67         });
 68        builder.setNegativeButton("取消", null);       
 69        Dialog dialog = builder.create();
 70        dialog.show();*/
 71            
 72           //Dialog实现3 Dialog 的链式调用实现   
 73           // Builder builder = 
 74              new Builder(this)  
 75             .setTitle("Android 培训")
 76             .setMessage("是否浏览王道论坛?")
 77             .setPositiveButton("确认", new OnClickListener() {    
 78             @Override
 79             public void onClick(DialogInterface dialog, int which) {
 80                 // TODO Auto-generated method stub
 81                 Intent intent = new Intent();
 82                 intent.setAction(Intent.ACTION_VIEW);
 83                 Uri uri =Uri.parse("http://www.cskaoyan.com/");
 84                 intent.setData(uri);
 85                 //定义的intent里面么有规定type
 86                 startActivity(intent);                
 87                 /*Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.cskaoyan.com/"));        
 88                 startActivity(intent);*/
 89             }
 90             })
 91             .setNegativeButton("取消", null)   
 92             .show();    
 93       // Toast.makeText(context, text, duration);
 94     }
 95      
 96     
 97     public void showDialog2(View v){        
 98          final String[] items = {"c","java" ,"c++"};        
 99         new Builder(this)
100         .setTitle("请选择学习的内容")
101         .setItems(items, new OnClickListener() {
102             
103             @Override
104             public void onClick(DialogInterface dialog, int which) {
105                 // TODO Auto-generated method stub                        
106                 System.out
107                         .println("MainActivity.showDialog2(...).new OnClickListener() {...}.onClick()"+which);                            
108                 String choice = items[which];            
109                 Toast.makeText(MainActivity.this, choice, 1).show();
110             }
111         })
112         .show();
113     }
114     
115     public void showDialog3(View v){        
116          final String[] items = {"c","java" ,"c++"};        
117         new Builder(this)
118         .setTitle("请选择学习的内容")
119         .setSingleChoiceItems(items, -1, new OnClickListener() {
120             
121             @Override
122             public void onClick(DialogInterface dialog, int which) {
123                 // TODO Auto-generated method stub                
124                 dialog.cancel();
125                 Toast.makeText(MainActivity.this, items[which], 1).show();                        
126             }
127         })
128         .show();
129         
130     }
131     
132     
133     public void showDialog4(View v){        
134         final String[] items = {"c","java" ,"c++"};         
135         new Builder(this)
136         .setTitle("请选择学习的内容")
137           .setMultiChoiceItems(items, new boolean[]{true,false,true}, new OnMultiChoiceClickListener() {
138             
139             @Override
140             public void onClick(DialogInterface dialog, int which, boolean isChecked) {
141                 // TODO Auto-generated method stub            
142                  if (isChecked) {
143                     Toast.makeText(MainActivity.this, items[which], 1).show();                        
144                 }
145             }
146         })
147         .setPositiveButton("确定", new OnClickListener() {
148             
149             @Override
150             public void onClick(DialogInterface dialog, int which) {
151                 // TODO Auto-generated method stub
152                 dialog.cancel();
153             }
154         })
155         .show();        
156     }
157     
158     public void showDialog5(View v){
159         
160      final  ProgressDialog dialog =    ProgressDialog.show(this, "警告", "我们正在升级系统,并不要乱按按钮。耐心等待");
161       
162       new Thread(new Runnable() {
163         
164         @Override
165         public void run() {
166             // TODO Auto-generated method stub
167             /*try {
168                 Thread.sleep(2000);
169             } catch (InterruptedException e) {
170                 // TODO Auto-generated catch block
171                 e.printStackTrace();
172             }*/        
173             SystemClock.sleep(2000);
174             dialog.cancel();    
175         }
176      }).start();      
177     }
178     
179     public void showDialog6(View v){
180          final  ProgressDialog dialog =    new ProgressDialog(this);
181          dialog.setTitle("提示");
182          dialog.setMessage("我们正在下载,请耐心等待");
183          dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
184          dialog.setMax(100);
185          
186          dialog.show();
187               
188          new Thread(new Runnable() {        
189                 @Override
190                 public void run() {
191                 int i = 0;
192                 while(true){
193                     
194                     //ProgressBar 这种控件是可以在子线程里更新UI的
195                     dialog.setProgress(i++);
196                     SystemClock.sleep(100);
197                     if (i>100) {
198                         break;
199                     }                    
200                 }
201                 dialog.cancel();
202             
203                 }
204              }).start();
205     }    
206 }

 

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:orientation="vertical"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:paddingBottom="@dimen/activity_vertical_margin"
 7     android:paddingLeft="@dimen/activity_horizontal_margin"
 8     android:paddingRight="@dimen/activity_horizontal_margin"
 9     android:paddingTop="@dimen/activity_vertical_margin"
10     tools:context="com.example.day20_01dialogdemo.MainActivity" >
11 
12     <TextView
13         android:layout_width="wrap_content"
14         android:layout_height="wrap_content"
15         android:text="@string/hello_world" />
16     
17     <Button
18         android:layout_width="wrap_content"
19         android:layout_height="wrap_content"
20         android:text="对话框1" 
21         android:onClick="showDialog1" />
22         
23     <Button
24         android:layout_width="wrap_content"
25         android:layout_height="wrap_content"
26         android:text="对话框2" 
27         android:onClick="showDialog2" />
28           
29     <Button
30         android:layout_width="wrap_content"
31         android:layout_height="wrap_content"
32         android:text="单选对话框" 
33         android:onClick="showDialog3"  />
34 
35     <Button
36         android:layout_width="wrap_content"
37         android:layout_height="wrap_content"
38         android:text="多选对话框" 
39         android:onClick="showDialog4" />
40                     
41     <Button
42         android:layout_width="wrap_content"
43         android:layout_height="wrap_content"
44         android:text="等待对话框" 
45         android:onClick="showDialog5" />
46           
47     <Button
48         android:layout_width="wrap_content"
49         android:layout_height="wrap_content"
50         android:text="进度对话框" 
51         android:onClick="showDialog6"/>
52 </LinearLayout>

 

020_01UI组件之Dialog详细用法

标签:

原文地址:http://www.cnblogs.com/woodrow2015/p/4544784.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!