The use of Android calendar/time/timer simple controls

When it comes to Android controls, compared to what Xiaobai thinks of, most of them are textview, imageview, edittext, button, checkbox and other commonly used controls. For some special functions that need to be displayed, it is not enough to express well. Today, it is relatively good to introduce a few A little bit of control to let Xiaobai show better in Android performance!

Today I will briefly introduce three, namely 1: datepicker calendar 2: timepicker clock 3: chronometer timer

The calendar simply realizes clicking the corresponding day toast to display the specific year, month and day, the clock realizes the rotation time toast to display the corresponding hour and minute, and the timer realizes timing for one minute, and stops immediately after one minute, and the number does not change anymore!

Below I will post the code directly (I wrote all three in a class file for easy browsing, and the xml layout is not shown here), because after all, it is relatively easy for controls, and some of them need to be explained. Relative comments are also made in the code segment. If you have any questions, you can leave a message in the comment area or private message me. If there are deficiencies or mistakes in the content, please correct me!

public class MainActivity extends AppCompatActivity {
private DatePicker datePicker;
private TimePicker timePicker;
private Chronometer chronometer;
private int year,mounth,day;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        datePicker=findViewById(R.id.datapicker);
        timePicker=findViewById(R.id.timepicker);
        chronometer=findViewById(R.id.ch);
        Calendar calendar=Calendar.getInstance();
        year=calendar.get(Calendar.YEAR);
        mounth=calendar.get(Calendar.MONTH);
        day=calendar.get(calendar.DAY_OF_MONTH);
        datePicker.init(year, mounth, day, new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                MainActivity.this.year=year;
                MainActivity.this.mounth=monthOfYear;
                MainActivity.this.day=dayOfMonth;
                String str1=year+"年"+(mounth+1)+"月"+day+"日";
                Toast.makeText(MainActivity.this,str1,Toast.LENGTH_SHORT).show();
            }
        });
        timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
            @Override
            public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
             timePicker.setIs24HourView(true);//24小时进制,不添加该行代码则时间默认为12小时,前方会有上午/下午
                String str2 =hourOfDay+"时"+minute+"分";
                Toast.makeText(MainActivity.this,str2,Toast.LENGTH_SHORT).show();
            }
        });
       /* setBase()>>>>>设置计时器的起始时间
        setFormat()>>>>>设置显示时间格式
        start()>>>>>指定开始时间
        stop()>>>>>指定停止时间
        setOnChronometerTickListener()计时器监听事件>>>>>*/
        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){//真实的时间减去开始的时间为60000毫秒即60秒时
                    chronometer.stop();
                }
            }
        });
    }
}

 

Guess you like

Origin blog.csdn.net/Abtxr/article/details/125831517