zoukankan      html  css  js  c++  java
  • android中提示&对话框----ProgressDialog&DatePickerDialog &TimePickerDialog&PopupWindow

    ProgressDialog(精度条对话框):

    1.直接调用ProgressDialog提供的静态方法show()显示

    2.创建ProgressDialog,再设置对话框的参数,最后show()出来

    package com.example.test3;
    
    import android.app.Activity;
    import android.app.ProgressDialog;
    import android.content.Context;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.view.View;
    import android.widget.Button;
    
    public class MainActivity extends Activity implements View.OnClickListener{
    
        private Button btn_one;
        private Button btn_two;
        private Button btn_three;
        private ProgressDialog pd1 = null;
        private ProgressDialog pd2 = null;
        private final static int MAXVALUE = 100;
        private int progressStart = 0;
        private int add = 0;
        private Context mContext = null;
    
    
        //定义一个用于更新进度的Handler,因为只能由主线程更新界面,所以要用Handler传递信息
        final Handler hand = new Handler()
        {
            @Override
            public void handleMessage(Message msg) {
                //这里的话如果接受到信息码是123
                if(msg.what == 123)
                {
                    //设置进度条的当前值
                    pd2.setProgress(progressStart);
                }
                //如果当前大于或等于进度条的最大值,调用dismiss()方法关闭对话框
                if(progressStart >= MAXVALUE)
                {
                    pd2.dismiss();
                }
            }
        };
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mContext = MainActivity.this;
            bindViews();
        }
    
        private void bindViews() {
            btn_one = (Button) findViewById(R.id.btn1);
            btn_two = (Button) findViewById(R.id.btn2);
            btn_three = (Button) findViewById(R.id.btn3);
            btn_one.setOnClickListener(this);
            btn_two.setOnClickListener(this);
            btn_three.setOnClickListener(this);
        }
    
    
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.btn1:
                    //这里的话参数依次为,上下文,标题,内容,是否显示进度,是否可以用取消按钮关闭
                    ProgressDialog.show(MainActivity.this, "资源加载中", "资源加载中,请稍后...",false,true);
                    break;
                case R.id.btn2:
                    pd1 = new ProgressDialog(mContext);
                    //依次设置标题,内容,是否用取消按钮关闭,是否显示进度
                    pd1.setTitle("软件更新中");
                    pd1.setMessage("软件正在更新中,请稍后...");
                    pd1.setCancelable(true);
                    //这里是设置进度条的风格,HORIZONTAL是水平进度条,SPINNER是圆形进度条
                    pd1.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                    pd1.setIndeterminate(true);
                    //调用show()方法将ProgressDialog显示出来
                    pd1.show();
                    break;
                case R.id.btn3:
                    //初始化属性
                    progressStart = 0;
                    add = 0;
                    //依次设置一些属性
                    pd2 = new ProgressDialog(MainActivity.this);
                    pd2.setMax(MAXVALUE);
                    pd2.setTitle("文件读取中");
                    pd2.setMessage("文件加载中,请稍后...");
                    //这里设置为不可以通过按取消按钮关闭进度条
                    pd2.setCancelable(false);
                    pd2.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                    //这里设置的是是否显示进度,设为false才是显示的哦!
                    pd2.setIndeterminate(false);
                    pd2.show();
                    //这里的话新建一个线程,重写run()方法,
                    new Thread()
                    {
                        public void run()
                        {
                            while(progressStart < MAXVALUE)
                            {
                                //这里的算法是决定进度条变化的,可以按需要写
                                progressStart = 2 * usetime() ;
                                //把信息码发送给handle让更新界面
                                hand.sendEmptyMessage(123);
                            }
                        }
                    }.start();
                    break;
            }
        }
    
        //这里设置一个耗时的方法:
        private int usetime() {
            add++;
            try{
                Thread.sleep(100);
            }catch (InterruptedException e) {
                e.printStackTrace();
            }
            return add;
        }
    }

    2.Date/TimePickerDialog只是供用户来选择日期时间,对于android系统的系统时间, 日期没有任何影响

    他们两个的构造方法非常相似: DatePickerDialog(上下文;DatePickerDialog.OnDateSetListener()监听器;年;月;日)
    TimePickerDialog(上下文;TimePickerDialog.OnTimeSetListener()监听器;小时,分钟,是否采用24小时制)

    public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    
        private Button btn_date;
        private Button btn_time;
        private String result = "";
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            bindViews();
        }
    
        private void bindViews() {
            btn_date = (Button) findViewById(R.id.btn_date);
            btn_time = (Button) findViewById(R.id.btn_time);
    
            btn_date.setOnClickListener(this);
            btn_time.setOnClickListener(this);
        }
    
        @Override
        public void onClick(View v) {
            result = "";
            switch (v.getId()){
                case R.id.btn_date:
                    Calendar cale1 = Calendar.getInstance();
                    new DatePickerDialog(MainActivity.this,new DatePickerDialog.OnDateSetListener() {
                        @Override
                        public void onDateSet(DatePicker view, int year, int monthOfYear,
                                              int dayOfMonth) {
                            //这里获取到的月份需要加上1哦~
                            result += "你选择的是"+year+"年"+(monthOfYear+1)+"月"+dayOfMonth+"日";
                            Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT).show();
                        }
                    }
                            ,cale1.get(Calendar.YEAR)
                            ,cale1.get(Calendar.MONTH)
                            ,cale1.get(Calendar.DAY_OF_MONTH)).show();
                    break;
                case R.id.btn_time:
                    Calendar cale2 = Calendar.getInstance();
                    new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
                        @Override
                        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                            result = "";
                            result += "您选择的时间是:"+hourOfDay+"时"+minute+"分";
                            Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT).show();
                        }
                    }, cale2.get(Calendar.HOUR_OF_DAY), cale2.get(Calendar.MINUTE), true).show();
                    break;
            }
        }
    }

    最后一个用于显示信息的UI控件——PopupWindow(悬浮框),如果你想知道 他长什么样子,你可以打开你手机的QQ,长按列表中的某项,这个时候后弹出一个黑色的小 对话框,这种就是PopupWindow了,和AlertDialog对话框不同的是,他的位置可以是随意的; 另外AlertDialog是非堵塞线程的,而PopupWindow则是堵塞线程的

    1)几个常用的构造方法

    我们在文档中可以看到,提供给我们的PopupWindow的构造方法有九种之多,这里只贴实际 开发中用得较多的几个构造方法:

    • public PopupWindow (Context context)
    • public PopupWindow(View contentView, int width, int height)
    • public PopupWindow(View contentView)
    • public PopupWindow(View contentView, int width, int height, boolean focusable)

    参数就不用多解释了吧,contentView是PopupWindow显示的View,focusable是否显示焦点


    2)常用的一些方法

    下面介绍几个用得较多的一些方法,其他的可自行查阅文档:

    • setContentView(View contentView):设置PopupWindow显示的View
    • getContentView():获得PopupWindow显示的View
    • showAsDropDown(View anchor):相对某个控件的位置(正左下方),无偏移
    • showAsDropDown(View anchor, int xoff, int yoff):相对某个控件的位置,有偏移
    • showAtLocation(View parent, int gravity, int x, int y): 相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移 PS:parent这个参数只要是activity中的view就可以了!
    • setWidth/setHeight:设置宽高,也可以在构造方法那里指定好宽高, 除了可以写具体的值,还可以用WRAP_CONTENT或MATCH_PARENT, popupWindow的width和height属性直接和第一层View相对应。
    • setFocusable(true):设置焦点,PopupWindow弹出后,所有的触屏和物理按键都由PopupWindows 处理。其他任何事件的响应都必须发生在PopupWindow消失之后,(home 等系统层面的事件除外)。 比如这样一个PopupWindow出现的时候,按back键首先是让PopupWindow消失,第二次按才是退出 activity,准确的说是想退出activity你得首先让PopupWindow消失,因为不并是任何情况下按back PopupWindow都会消失,必须在PopupWindow设置了背景的情况下 。
    • setAnimationStyle(int):设置动画效果
    public class MainActivity extends Activity {
    
        private Button btn_show;
        private Context mContext;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mContext = MainActivity.this;
            btn_show = (Button) findViewById(R.id.btn_show);
            btn_show.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    initPopWindow(v);
                }
            });
        }
    
    
        private void initPopWindow(View v) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_popup, null, false);
            Button btn_xixi = (Button) view.findViewById(R.id.btn_xixi);
            Button btn_hehe = (Button) view.findViewById(R.id.btn_hehe);
            //1.构造一个PopupWindow,参数依次是加载的View,宽高
            final PopupWindow popWindow = new PopupWindow(view,
                    ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
    
            popWindow.setAnimationStyle(R.anim.anim_pop);  //设置加载动画
    
            //这些为了点击非PopupWindow区域,PopupWindow会消失的,如果没有下面的
            //代码的话,你会发现,当你把PopupWindow显示出来了,无论你按多少次后退键
            //PopupWindow并不会关闭,而且退不出程序,加上下述代码可以解决这个问题
            popWindow.setTouchable(true);
            popWindow.setTouchInterceptor(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    return false;
                    // 这里如果返回true的话,touch事件将被拦截
                    // 拦截后 PopupWindow的onTouchEvent不被调用,这样点击外部区域无法dismiss
                }
            });
            popWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));    //要为popWindow设置一个背景才有效
    
    
            //设置popupWindow显示的位置,参数依次是参照View,x轴的偏移量,y轴的偏移量
            popWindow.showAsDropDown(v, 50, 0);
    
            //设置popupWindow里的按钮的事件
            btn_xixi.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(MainActivity.this, "你点击了嘻嘻~", Toast.LENGTH_SHORT).show();
                }
            });
            btn_hehe.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(MainActivity.this, "你点击了呵呵~", Toast.LENGTH_SHORT).show();
                    popWindow.dismiss();
                }
            });
        }
    }
  • 相关阅读:
    day16(链表中倒数第k个结点)
    day15(C++格式化输出数字)
    day14(调整数组顺序使奇数位于偶数前面 )
    day13(数值的整数次)
    day12(二进制中1的个数)
    day11(矩形覆盖)
    day10(跳台阶)
    hadoop 又一次环境搭建
    Hive 学习
    hadoop -工具合集
  • 原文地址:https://www.cnblogs.com/huolan/p/5155315.html
Copyright © 2011-2022 走看看