时间日期选择器 计时器

2019-12-01  本文已影响0人  琳媚儿

日期

直接创建一个DatePickerDialog对话框实例,并将它显示出

private void showData(MainActivity mainActivity, int i, Button button, Calendar calendar) {
       Calendar calendar=Calendar.getInstance(Locale.CHINA);
        // // 直接创建一个DatePickerDialog对话框实例,并将它显示出
        DatePickerDialog dialog=new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker datePicker, int year, int month, int data) {
                textView.setText(year+"年"+month+"月"+data+"日");
            }
        }, this.calendar.get(Calendar.YEAR), this.calendar.get(Calendar.MONTH), this.calendar.get(Calendar.DAY_OF_MONTH));
        dialog.show();
    }
    <DatePicker
        android:id="@+id/picker"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"></DatePicker>
 Calendar calendar=Calendar.getInstance();
        year=calendar.get(Calendar.YEAR);
        month=calendar.get(Calendar.MONTH);
        data=calendar.get(Calendar.DAY_OF_MONTH);
        picker.init(year, month, data, new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker datePicker, int i, int i1, int i2) {
               year=i;
               month=i1;
               data=i2;
               show(year,month,data);
            }
        });
   
2019050711093388.png

时间

<TimePicker
        android:id="@+id/Time"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

    </TimePicker>
timePicker = findViewById(R.id.Time);
        timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
            @Override
            public void onTimeChanged(TimePicker timePicker, int hour, int minute) {
                String str = hour + "时" + minute + "分";
                Toast.makeText(Time2Activity.this, str, Toast.LENGTH_LONG).show();
            }
        });
 public void showTime(MainActivity mainActivity, int i, Button button1, Calendar calendar){
        // Calendar c = Calendar.getInstance();
        // 创建一个TimePickerDialog实例,并把它显示出来
        // 解释一哈,Activity是context的子类
        this.calendar =Calendar.getInstance();
        TimePickerDialog timePickerDialog=new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker timePicker, int hour, int minute) {
                textView1.setText(hour+"时"+minute+"分");
            }
        }
        , this.calendar.get(Calendar.HOUR_OF_DAY)
                , this.calendar.get(Calendar.MINUTE)
                ,true
        );
        timePickerDialog.show();
    }
20190507110942998.png

计时器

<Chronometer
    android:id="@+id/ch"
    android:layout_gravity="center"
    android:textSize="25sp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"></Chronometer>
  chronometer=findViewById(R.id.ch);
        chronometer.setBase(SystemClock.elapsedRealtime());
        chronometer.setFormat("%s");
        chronometer.start();
        chronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
            @Override
            public void onChronometerTick(Chronometer chronometer) {
                if (SystemClock.elapsedRealtime()-chronometer.getBase()>60000){
                    chronometer.stop();
                }
            }
        });
上一篇下一篇

猜你喜欢

热点阅读