Android开发之对话框案例详解(五种对话框)

Android开发之对话框案例详解(五种对话框),第1张

概述下面通过实例代码给大家分享5种android对话框,具体内容详情如下所示:1d出普通对话框---系统更新

下面通过实例代码给大家分享5种androID对话框,具体内容详情如下所示:

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

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

3 时间选择对话框 -- 时间对话框

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

5 popuWindow对话框

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

//d出普通对话框    public voID shownormalDialog(VIEw v) {     AlertDialog.Builder builder = new Builder(this);     //设置Dialog的图标     builder.setIcon(R.drawable.ic_launcher);     //设置对话框的标题     builder.setTitle("更新");     //设置message     builder.setMessage("发现新版本是否更新?");     //确定按钮  取消按钮     builder.setPositivebutton("确定",new OnClickListener() {       /**        * 点击确定按钮 回调该方法        */       @OverrIDe       public voID onClick(DialogInterface dialog,int which) {         //到服务器去下载新的版本 duration单词意思:时长         Toast.makeText(MainActivity.this,"开始下载新版本",Toast.LENGTH_SHORT).show();       }     });     builder.setNegativebutton("取消",new OnClickListener() {       /**        * 点击取消按钮 回调该方法        */       @OverrIDe       public voID onClick(DialogInterface dialog,"不需要更新",Toast.LENGTH_SHORT).show();       }     });     builder.setNeutralbutton("下一次",new OnClickListener() {       @OverrIDe       public voID onClick(DialogInterface dialog,"下一次吧",Toast.LENGTH_SHORT).show();       }     });     //通过建造这老构建一个对话框     Dialog dialog = builder.create();     //显示     dialog.show();   } 

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

  布局文件:

  user_name_dialog.xml

<?xml version="1.0" enCoding="utf-8"?> <linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"   androID:layout_wIDth="match_parent"   androID:layout_height="match_parent"   androID:padding="10dip"   androID:orIEntation="vertical" >   <TextVIEw       androID:ID="@+ID/tv_Title"       androID:layout_wIDth="match_parent"       androID:layout_height="wrap_content"       androID:text="登录信息"       androID:gravity="center"       androID:textAppearance="?androID:attr/textAppearanceLarge" />   <TextVIEw     androID:ID="@+ID/tv_name"     androID:layout_wIDth="wrap_content"     androID:layout_height="wrap_content"     androID:text="用户名:" />   <EditText androID:ID="@+ID/et_name"     androID:layout_wIDth="match_parent"     androID:layout_height="wrap_content"     androID:hint="请输入用户名"/>   <TextVIEw     androID:ID="@+ID/tv_pwd"     androID:layout_wIDth="wrap_content"     androID:layout_height="wrap_content"     androID:text="密 码:" />   <EditText androID:ID="@+ID/et_pwd"     androID:layout_wIDth="match_parent"     androID:layout_height="wrap_content"     androID:inputType="textPassword"     androID:hint="请输入密码"/>     <requestFocus />     <button       androID:ID="@+ID/btn_confirm"       androID:layout_wIDth="150dip"       androID:layout_height="wrap_content"       androID:layout_gravity="center"       androID:text="登录" />     <button       androID:ID="@+ID/btn_cancel"       androID:layout_wIDth="150dip"       androID:layout_height="wrap_content"       androID:layout_gravity="center"       androID:text="取消" /> </linearLayout> 

 java代码: 

//自定义对话框   Dialog cus_dialog ;   public voID showCustomDialog(VIEw v){     AlertDialog.Builder builder = new Builder(this);     // 布局填充器     LayoutInflater inflater = LayoutInflater.from(this);     VIEw vIEw = inflater.inflate(R.layout.user_name_dialog,null);     // 设置自定义的对话框界面     builder.setVIEw(vIEw);     // 获取用户名密码     final EditText name = (EditText) vIEw.findVIEwByID(R.ID.et_name);     final EditText pwd = (EditText) vIEw.findVIEwByID(R.ID.et_pwd);     button btn_confirm = (button) vIEw.findVIEwByID(R.ID.btn_confirm);     btn_confirm.setonClickListener(new VIEw.OnClickListener() {       @OverrIDe       public voID onClick(VIEw v) {         // Todo 自动生成的方法存根         if(name.getText().toString().trim().equals("abc")){           showToastMsg("用户名正确");           // 对话框消失           cus_dialog.dismiss();         }         else{           showToastMsg("用户名错误");         }       }     });     button btnCancel = (button) vIEw.findVIEwByID(R.ID.btn_cancel);     btnCancel.setonClickListener(new VIEw.OnClickListener() {       @OverrIDe       public voID onClick(VIEw v) {         // 对话框消失         cus_dialog.dismiss();       }     });     cus_dialog = builder.create();     cus_dialog.show();   } 

 下载地址:http://www.jinhusns.com/Products/Download/?type=yhq

3 时间选择对话框 -- 时间对话框

// 时间选择对话框 public voID showTimePickerDialog(VIEw v){   Calendar sysDate = Calendar.getInstance();   //设置系统时间   sysDate.setTimeInMillis(System.currentTimeMillis());   int hour = sysDate.get(Calendar.HOUR_OF_DAY);   int minute = sysDate.get(Calendar.MINUTE);   TimePickerDialog time = new TimePickerDialog(this,new OnTimeSetListener() {         @OverrIDe         public voID onTimeSet(TimePicker vIEw,int hourOfDay,int minute) {           // Todo 自动生成的方法存根           showToastMsg(" hourOfDay:" + hourOfDay + " minute:" + minute);         }       },//callBack 选择时间后的回调方法       hour,//hourOfDay 当前系统时间       minute,//hourOfDay 当前系统时间       true);//是否24小时制   time.show(); } 

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

/**    * 进度条对话框    * @param v    */   public voID showProgressDialog(VIEw v){     final ProgressDialog progress = new ProgressDialog(this);     progress.setProgress(R.drawable.img2);     progress.setTitle("标题");     progress.setMessage("加载中...");     //样式1 进度条样式     progress.setProgressstyle(ProgressDialog.STYLE_HORIZONTAL);     //样式2 有加载图标     //progress.setProgressstyle(ProgressDialog.STYLE_SPINNER);     //最大     progress.setMax(100);     //当前     progress.setProgress(50);     progress.setbutton("确定",int which) {         // Todo 自动生成的方法存根         showToastMsg("确定按钮");       }     });     progress.setbutton2("取消",int which) {         // Todo 自动生成的方法存根         showToastMsg("取消按钮");       }     });     progress.show();      new Thread(new Runnable() {        @OverrIDe        public voID run() {          // Todo auto-generated method stub          int i = 0;          while (i < 100) {            try {              Thread.sleep(200);              // 更新进度条的进度,可以在子线程中更新进度条进度              progress.incrementProgressBy(5);              // progress.incrementSecondaryProgressBy(10);//二级进度条更新方式              i += 5;            } catch (Exception e) {              // Todo: handle exception            }          }          // 在进度条走完时删除Dialog          progress.dismiss();        }      }).start();    } 

5 popuWindow对话框

button btn_popu;   //popuWindow对话框   public voID showPopuWindow(VIEw v){     btn_popu = (button) v;     // 设置布局     VIEw vIEw = LayoutInflater.from(this).inflate(R.layout.pop_window,null);     PopupWindow window = new PopupWindow(this);     window.setContentVIEw(vIEw);     window.setWIDth(360);     window.setHeight(200);     int[] location = new int[2];     // 获取按钮坐标     btn_popu.getLocationInWindow(location);     window.setFocusable(true);     window.setBackgroundDrawable(getResources().getDrawable(R.drawable.back_null));     window.showAtLocation(btn_popu,Gravity.left |Gravity.top,location[0]+ btn_popu.getWIDth(),location[1] + 0 );     //showToastMsg("" + (location[0]+ btn_popu.getWIDth())+"  "+ (location[1] + btn_popu.getHeight() / 2));     ImageVIEw img_start = (ImageVIEw) vIEw.findVIEwByID(R.ID.img_start);     img_start.setonClickListener(new VIEw.OnClickListener() {       @OverrIDe       public voID onClick(VIEw v) {         // Todo 自动生成的方法存根         showToastMsg("点击了启动");       }     });   } 

总结

以上所述是小编给大家介绍的详解AndroID开发之对话框案例详解(五种对话框),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!

总结

以上是内存溢出为你收集整理的Android开发之对话框案例详解(五种对话框)全部内容,希望文章能够帮你解决Android开发之对话框案例详解(五种对话框)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1144490.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存