原创

Dialog方法实现


AlertDialog对话框

  1. 默认样式实现,new AlertDialog对象
 new AlertDialog.Builder(this)
                    .setTitle("删除数据")
                    .setMessage("你确定删除数据吗?")
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
//                        Toast.makeText(DialogActivity.this,"删除数据",0).show();
                        }
                    })
                    .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
//                        Toast.makeText(DialogActivity.this,"取消删除数据",0).show();
                        }
                    })
                    .show();//方法链调用
  1. 显示单选列表AlertDialog
  public void DialogDan(){//单选列表
        final String[] items={"红","蓝","绿","灰"};
            new AlertDialog.Builder(this)
                    .setTitle("指定背景颜色")
                    .setSingleChoiceItems(items, 2, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {//which就是选中的position
                        Toast.makeText(DialogActivity.this,items[which],which).show();
                        //移除
                        }
                    })
                    .show();
        }

3.自定义AlerDialog

 public void DialogZ(View v){//自定义Dialog
            View view=View.inflate(this,R.layout.item_dialog,null);
            final EditText nameEt=view.findViewById(R.id.item_name);
            final EditText namepw=view.findViewById(R.id.item_pass);
            new AlertDialog.Builder(this)
                    .setTitle("自定义Dailog")
                    .setView(view)
                    .setNegativeButton("取消",null)
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            //读取用户名密码
                            String username=nameEt.getText().toString();
                            String userpass=namepw.getText().toString();
                            //提示
                            Toast.makeText(DialogActivity.this,"用户名:"+username+"密 码:"+userpass,which).show();

                        }
                    })
                    .show();
        }

4.圆形进度条AlerDialog

 public void Dialog_Progress(final View view){
            final ProgressDialog show = ProgressDialog.show(this, "数据加载", "数据加载中....");
            new Thread(){//长时间工作不能再主线程做,得启动分线程
                @Override
                public void run() {
                    for (int i=0;i<20;i++){
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    //移除Dialog
                    show.dismiss();
                    //显示Toast
                   runOnUiThread(new Runnable() {//不能再分线程启动UI
                       @Override
                       public void run() {
                           Toast.makeText(DialogActivity.this,"加载完成",Toast.LENGTH_SHORT).show();
                       }
                   });
                }
            }.start();

        }

5.水平进度条AlerDialog

 public void Dialog_pard(){
        //1\创建dialog对象
            final ProgressDialog pd=new ProgressDialog(this);
            //2.设置样式
            pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            //设置进度条最大值
            pd.setMax(20);
            //3。显示
            pd.show();
            //线程启动
            new Thread(new Runnable() {
                @Override
                public void run() {
                    int count=20;
                    //设置最大进度
                    for (int i=0;i<count;i++){
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        pd.setProgress(pd.getProgress()+1);
                    }
                    pd.dismiss();
                }
            }).start();
        }

6.显示日期AlerDialog

  public void DialogData(){
        //创建日历
            Calendar calendar=Calendar.getInstance();
            final int year=calendar.get(Calendar.YEAR);
            final int month=calendar.get(Calendar.MONTH);
            final int dayOfMonth=calendar.get(Calendar.DAY_OF_MONTH);
        new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
            }
        },year,month,dayOfMonth).show();
        }

7.显示时间Tiem

 public void DialogItem(){
            //创建时间
            Calendar c=Calendar.getInstance();
            final int hour=c.get(Calendar.HOUR_OF_DAY);
            final int minute=c.get(Calendar.MINUTE);
            new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
                @Override
                public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

                }
            },hour,minute,true).show();
        }
日常学习
  • 作者:杜兄(联系作者)
  • 发表时间:2019-10-20 19:13
  • 版权声明:自由转载-非商用-非衍生-保持署名(创意共享3.0许可证)
  • 公众号转载:请在文末添加作者公众号二维码
  • 评论