你的位置:首页 > 软件开发 > 操作系统 > Android开发笔记: 5种对话框案例

Android开发笔记: 5种对话框案例

发布时间:2017-09-12 19:00:15
5种android对话框1 弹出普通对话框 --- 系统更新2 自定义对话框-- 用户登录3 时间选择对话框 -- 时间对话框4 进度条对话框 -- 信息加载..5 popuWindow对话框 1 弹出普通对话框 --- 系统更新 [csharp] view p ...

5种android对话框
1 弹出普通对话框 --- 系统更新
2 自定义对话框-- 用户登录
3 时间选择对话框 -- 时间对话框
4 进度条对话框 -- 信息加载..
5 popuWindow对话框

 

1 弹出普通对话框 --- 系统更新

 

 

[csharp] view plain copy 
  1. //弹出普通对话框   
  2.     public void showNormalDialog(View v) {  
  3.           
  4.         AlertDialog.Builder builder = new Builder(this);  
  5.         //设置Dialog的图标  
  6.         builder.setIcon(R.drawable.ic_launcher);  
  7.         //设置对话框的标题  
  8.         builder.setTitle("更新");  
  9.         //设置message  
  10.         builder.setMessage("发现新版本是否更新?");  
  11.         //确定按钮   取消按钮  
  12.         builder.setPositiveButton("确定",new OnClickListener() {  
  13.             /** 
  14.              * 点击确定按钮 回调该方法 
  15.              */  
  16.             @Override  
  17.             public void onClick(DialogInterface dialog, int which) {  
  18.                 //到服务器去下载新的版本 duration单词意思:时长  
  19.                 Toast.makeText(MainActivity.this, "开始下载新版本", Toast.LENGTH_SHORT).show();  
  20.             }  
  21.         });  
  22.         builder.setNegativeButton("取消", new OnClickListener() {  
  23.             /** 
  24.              * 点击取消按钮 回调该方法 
  25.              */  
  26.             @Override  
  27.             public void onClick(DialogInterface dialog, int which) {  
  28.                 //到服务器去下载新的版本 duration单词意思:时长  
  29.                 Toast.makeText(MainActivity.this, "不需要更新", Toast.LENGTH_SHORT).show();  
  30.                   
  31.             }  
  32.         });  
  33.         builder.setNeutralButton("下一次", new OnClickListener() {  
  34.               
  35.             @Override  
  36.             public void onClick(DialogInterface dialog, int which) {  
  37.                 //到服务器去下载新的版本 duration单词意思:时长  
  38.                 Toast.makeText(MainActivity.this, "下一次吧", Toast.LENGTH_SHORT).show();  
  39.                   
  40.             }  
  41.         });  
  42.         //通过建造这老构建一个对话框  
  43.         Dialog dialog = builder.create();  
  44.         //显示  
  45.         dialog.show();  
  46.       
  47.     }  

Android开发笔记: 5种对话框案例

 

 

2 自定义对话框-- 用户登录

 

  布局文件:

  user_name_dialog.

Android开发笔记: 5种对话框案例

[csharp] view plain copy 
  1. <?"1.0" encoding="utf-8"?>  
  2. <LinearLayout "http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:padding="10dip"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <TextView  
  9.             android:id="@+id/tv_title"  
  10.             android:layout_width="match_parent"  
  11.             android:layout_height="wrap_content"  
  12.             android:text="登录信息"  
  13.             android:gravity="center"  
  14.             android:textAppearance="?android:attr/textAppearanceLarge" />  
  15.        
  16.       
  17.     <TextView  
  18.         android:id="@+id/tv_name"  
  19.         android:layout_width="wrap_content"  
  20.         android:layout_height="wrap_content"  
  21.         android:text="用户名:" />  
  22.       
  23.     <EditText android:id="@+id/et_name"  
  24.         android:layout_width="match_parent"  
  25.         android:layout_height="wrap_content"  
  26.         android:hint="请输入用户名"/>  
  27.       
  28.     <TextView  
  29.         android:id="@+id/tv_pwd"  
  30.         android:layout_width="wrap_content"  
  31.         android:layout_height="wrap_content"  
  32.         android:text="密 码:" />  
  33.       
  34.     <EditText android:id="@+id/et_pwd"  
  35.         android:layout_width="match_parent"  
  36.         android:layout_height="wrap_content"  
  37.         android:inputType="textPassword"  
  38.         android:hint="请输入密码"/>  
  39.    
  40.         <requestFocus />  
  41.   
  42.         <Button  
  43.             android:id="@+id/btn_confirm"  
  44.             android:layout_width="150dip"  
  45.             android:layout_height="wrap_content"  
  46.             android:layout_gravity="center"  
  47.             android:text="登录" />  
  48.   
  49.         <Button  
  50.             android:id="@+id/btn_cancel"  
  51.             android:layout_width="150dip"  
  52.             android:layout_height="wrap_content"  
  53.             android:layout_gravity="center"  
  54.             android:text="取消" />  
  55.           
  56.   
  57. </LinearLayout>  

 

 

 java代码:

 

[csharp] view plain copy 
  1. //自定义对话框  
  2.     Dialog cus_dialog ;  
  3.     public void showCustomDialog(View v){  
  4.         AlertDialog.Builder builder = new Builder(this);  
  5.         // 布局填充器  
  6.         LayoutInflater inflater = LayoutInflater.from(this);  
  7.         View view = inflater.inflate(R.layout.user_name_dialog, null);  
  8.           
  9.         // 设置自定义的对话框界面  
  10.         builder.setView(view);  
  11.           
  12.         // 获取用户名密码  
  13.         final EditText name = (EditText) view.findViewById(R.id.et_name);  
  14.         final EditText pwd = (EditText) view.findViewById(R.id.et_pwd);  
  15.         Button btn_confirm = (Button) view.findViewById(R.id.btn_confirm);  
  16.           
  17.         btn_confirm.setOnClickListener(new View.OnClickListener() {  
  18.               
  19.             @Override  
  20.             public void onClick(View v) {  
  21.                 // TODO 自动生成的方法存根  
  22.                 if(name.getText().toString().trim().equals("abc")){  
  23.                     showToastMsg("用户名正确");  
  24.                     // 对话框消失  
  25.                     cus_dialog.dismiss();  
  26.                 }  
  27.                 else{  
  28.                     showToastMsg("用户名错误");  
  29.                 }  
  30.             }  
  31.         });  
  32.           
  33.         Button btnCancel = (Button) view.findViewById(R.id.btn_cancel);  
  34.           
  35.         btnCancel.setOnClickListener(new View.OnClickListener() {  
  36.               
  37.             @Override  
  38.             public void onClick(View v) {  
  39.                 // 对话框消失  
  40.                 cus_dialog.dismiss();  
  41.             }  
  42.         });  
  43.         cus_dialog = builder.create();  
  44.         cus_dialog.show();  
  45.           
  46.           
  47.     }  

Android开发笔记: 5种对话框案例

 下载地址 时间选择对话框 -- 时间对话框

 

 

[csharp] view plain copy 
  1. // 时间选择对话框  
  2. public void showTimePickerDialog(View v){  
  3.       
  4.     Calendar sysDate = Calendar.getInstance();  
  5.     //设置系统时间  
  6.     sysDate.setTimeInMillis(System.currentTimeMillis());  
  7.     int hour = sysDate.get(Calendar.HOUR_OF_DAY);  
  8.     int minute = sysDate.get(Calendar.MINUTE);  
  9.       
  10.       
  11.     TimePickerDialog time = new TimePickerDialog(this,  
  12.             new OnTimeSetListener() {  
  13.                   
  14.                 @Override  
  15.                 public void onTimeSet(TimePicker view, int hourOfDay, int minute) {  
  16.                     // TODO 自动生成的方法存根  
  17.                     showToastMsg(" hourOfDay:" + hourOfDay + "  minute:" + minute);  
  18.                 }  
  19.             }, //callBack 选择时间后的回调方法  
  20.             hour,//hourOfDay 当前系统时间  
  21.             minute,//hourOfDay 当前系统时间  
  22.             true);//是否24小时制  
  23.       
  24.     time.show();  
  25. }  


Android开发笔记: 5种对话框案例

 

 

4 进度条对话框 -- 信息加载..

 

 

[csharp] view plain copy 
  1. /** 
  2.      * 进度条对话框 
  3.      * @param v 
  4.      */  
  5.     public void showProgressDialog(View v){  
  6.               
  7.         final ProgressDialog progress = new ProgressDialog(this);  
  8.         progress.setProgress(R.drawable.img2);  
  9.         progress.setTitle("标题");  
  10.           
  11.         progress.setMessage("加载中...");  
  12.         //样式1 进度条样式  
  13.         progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);  
  14.         //样式2 有加载图标  
  15.         //progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);  
  16.           
  17.         //最大  
  18.         progress.setMax(100);  
  19.         //当前  
  20.         progress.setProgress(50);  
  21.         progress.setButton("确定", new  OnClickListener() {  
  22.               
  23.             @Override  
  24.             public void onClick(DialogInterface dialog, int which) {  
  25.                 // TODO 自动生成的方法存根  
  26.                 showToastMsg("确定按钮");  
  27.             }  
  28.         });  
  29.           
  30.            
  31.         progress.setButton2("取消", new OnClickListener() {  
  32.               
  33.             @Override  
  34.             public void onClick(DialogInterface dialog, int which) {  
  35.                 // TODO 自动生成的方法存根  
  36.                 showToastMsg("取消按钮");  
  37.             }  
  38.         });  
  39.           
  40.         progress.show();   
  41.            
  42.         new Thread(new Runnable() {    
  43.                 
  44.             @Override    
  45.             public void run() {    
  46.                 // TODO Auto-generated method stub    
  47.                 int i = 0;    
  48.                 while (i < 100) {    
  49.                     try {    
  50.                         Thread.sleep(200);    
  51.                         // 更新进度条的进度,可以在子线程中更新进度条进度    
  52.                        progress.incrementProgressBy(5);    
  53.                        // progress.incrementSecondaryProgressBy(10);//二级进度条更新方式    
  54.                         i += 5;    
  55.         
  56.                     } catch (Exception e) {    
  57.                         // TODO: handle exception    
  58.                     }    
  59.                 }    
  60.                 // 在进度条走完时删除Dialog    
  61.                 progress.dismiss();    
  62.         
  63.             }    
  64.         }).start();    
  65.           
  66.     }  


Android开发笔记: 5种对话框案例

 

 

 

 

 

 

 

 

 

5 popuWindow对话框

 

 

[csharp] view plain copy 
  1. Button btn_popu;  
  2.     //popuWindow对话框  
  3.     public void showPopuWindow(View v){  
  4.           
  5.         btn_popu = (Button) v;  
  6.         // 设置布局  
  7.         View view = LayoutInflater.from(this).inflate(R.layout.pop_window, null);  
  8.           
  9.         PopupWindow window = new PopupWindow(this);  
  10.         window.setContentView(view);  
  11.         window.setWidth(360);  
  12.         window.setHeight(200);  
  13.   
  14.         int[] location = new int[2];  
  15.         // 获取按钮坐标  
  16.         btn_popu.getLocationInWindow(location);  
  17.         window.setFocusable(true);  
  18.         window.setBackgroundDrawable(getResources().getDrawable(R.drawable.back_null));  
  19.         window.showAtLocation(btn_popu, Gravity.LEFT |Gravity.TOP , location[0]+ btn_popu.getWidth(), location[1] + 0 );  
  20.           
  21.         //showToastMsg("" + (location[0]+ btn_popu.getWidth())+"   "+ (location[1] + btn_popu.getHeight() / 2));  
  22.           
  23.         ImageView img_start = (ImageView) view.findViewById(R.id.img_start);  
  24.         img_start.setOnClickListener(new View.OnClickListener() {  
  25.               
  26.             @Override  
  27.             public void onClick(View v) {  
  28.                 // TODO 自动生成的方法存根  
  29.                 showToastMsg("点击了启动");  
  30.             }  
  31.         });  
  32.           
  33.     }  


Android开发笔记: 5种对话框案例

原标题:Android开发笔记: 5种对话框案例

关键词:Android

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录