zoukankan      html  css  js  c++  java
  • 安卓学习-界面-ui-DatePickerDialog、TimePickerDialog和ProgressDialog

    1.DatePickerDialog和TimePickerDialog

    例子

      

    activity_main.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="${relativePackage}.${activityClass}" >
    
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/button1"
            android:layout_alignBottom="@+id/button1"
            android:layout_alignParentRight="true"
            android:layout_marginRight="80dp"
            android:text="时间" />
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginRight="50dp"
            android:layout_marginTop="32dp"
            android:layout_toLeftOf="@+id/button2"
            android:text="日期" />
    
    </RelativeLayout>
    View Code

    MainActivity.java

    public class MainActivity extends Activity {
        
        TextView tv1;
        TextView tv2;
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            
            Button btn1=(Button)findViewById(R.id.button1);
            Button btn2=(Button)findViewById(R.id.button2);
    
            
            btn1.setOnClickListener(new OnClickListener() {
                Calendar c=Calendar.getInstance();
                @Override
                public void onClick(View v) {
                    DatePickerDialog d=new DatePickerDialog(MainActivity.this,new OnDateSetListener() {
                        
                        @Override
                        public void onDateSet(DatePicker view, int year, int monthOfYear,
                                int dayOfMonth) {
                            Toast.makeText(MainActivity.this, "年"+year+":月"+(monthOfYear+1)+":日"+dayOfMonth, Toast.LENGTH_SHORT).show();
                        }
                    },c.get(Calendar.YEAR),c.get(Calendar.MONTH),c.get(Calendar.DAY_OF_MONTH));
                    d.show();
                }
            });
            
            btn2.setOnClickListener(new OnClickListener() {
                Calendar c=Calendar.getInstance();
                @Override
                public void onClick(View v) {
                    TimePickerDialog t=new TimePickerDialog(MainActivity.this, new OnTimeSetListener() {
                        
                        @Override
                        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                            // TODO 自动生成的方法存根
                            Toast.makeText(MainActivity.this, "时"+hourOfDay+":分"+(minute+1), Toast.LENGTH_SHORT).show();
                        }
                    }, c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE), true);
                    t.show();
                }
            });
        }
    }
    View Code

    2.ProgressDialog

      

    activity_main.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="${relativePackage}.${activityClass}" >
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="59dp"
            android:text="无进度条" />
    
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignRight="@+id/button1"
            android:layout_below="@+id/button1"
            android:layout_marginTop="33dp"
            android:text="带进度条" />
    
        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/button2"
            android:layout_below="@+id/button2"
            android:layout_marginTop="50dp"
            android:text="模拟耗时" />
    
    </RelativeLayout>
    View Code

    MainActivity

    public class MainActivity extends Activity {
    
        ProgressDialog p;
        int pos=0;
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            
            Button btn1=(Button)findViewById(R.id.button1);
            Button btn2=(Button)findViewById(R.id.button2);
            Button btn3=(Button)findViewById(R.id.button3);
    
            
            btn1.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    ProgressDialog.show(MainActivity.this, "任务执行中","任务执行中,请等待......", true,false);
                }
            });
            
            btn2.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    p=new ProgressDialog(MainActivity.this);
                    p.setTitle("任务执行中");
                    p.setMessage("任务执行中,请等待......");
                    //可以被取消
                    p.setCancelable(true);
                    //样式
                    //滚动    ProgressDialog.STYLE_SPINNER 
                    //水平进度    STYLE_HORIZONTAL
                    p.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                    p.setIndeterminate(true);
                    p.show();
                }
            });
            
            final Handler handler =new Handler(){
                @Override
                public void handleMessage(Message msg) {
                    if(msg.what==1234){
                        p.setProgress(pos);
                        if(pos==100){
                            Toast.makeText(MainActivity.this, "完成", Toast.LENGTH_SHORT).show();
                        }    
                    }
                }
            };
            
            btn3.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    p=new ProgressDialog(MainActivity.this);
                    p.setMax(100);
                    p.setMax(0);
                    p.setTitle("任务执行中");
                    p.setMessage("任务执行完成比,请等待......");
                    p.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                    p.setCancelable(false);
                    p.setIndeterminate(false);
                    p.show();
                    new Thread(){
                        public void run() {
                            try {
                                while(pos<=100){
                                    pos=pos+1;
                                    Message msg=new Message();
                                    msg.what=1234;
                                    handler.sendMessage(msg);
                                    sleep(100);
                                }    
                                p.dismiss();
                                
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }                        
                        };
                    }.start();
                    
                }
            });
        }
    }
    View Code
  • 相关阅读:
    python 迭代器&&生成器
    windows 10 扩大C盘空间
    robot framework 接口自动化测试和关键字开发
    Robot framework 环境搭建+图标处理
    Docker 安装-在centos7下安装Docker(二)
    win10系统rational rose 安装后打开弹框显示java.lang.ClassNotFoundException 解决方案
    mysql 关系表 分组读取的方法
    关于浮点型计算遇到的小问题
    dom 的介绍
    网站前端相关的知识点
  • 原文地址:https://www.cnblogs.com/weijj/p/4046129.html
Copyright © 2011-2022 走看看