日历,时钟,计时器
package com.example.mybaseuii;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.widget.Chronometer;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.Toast;
import java.util.Calendar;
public class MainActivity extends Activity {
int year,month,day;
DatePicker datePicker = null;
TimePicker timePicker = null;
Chronometer chronometer = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
datePicker = (DatePicker) findViewById(R.id.datepicker);
Calendar calendar = Calendar.getInstance();
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH);
day = calendar.get(Calendar.DAY_OF_MONTH);
//初始化日期选择器
datePicker.init(year, month, day, new DatePicker.OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
MainActivity.this.year=year;
MainActivity.this.month=day;
MainActivity.this.day=day;
show(year,month,day);
}
});
//时钟
timePicker = (TimePicker)findViewById(R.id.timepicker);
timePicker.setIs24HourView(true);//设置成24小时的
timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
String string = hourOfDay + "时" + minute + "分";
Toast.makeText(MainActivity.this,string,
Toast.LENGTH_SHORT).show();
}
});
//计时器
chronometer = (Chronometer) findViewById(R.id.chronometer);
//设置起始时间,获取系统时间
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();
}
}
});
}
private void show(int year,int month,int day){
String str = year+"年"+(month+1)+"月"+day+"日";
Toast.makeText(MainActivity.this,str,
Toast.LENGTH_SHORT).show();
}
}
调用方式
package com.example.mybaseuii; import androidx.appcompat.app.AppCompatActivity; import android.app.Activity; import android.os.Bundle; import android.os.SystemClock; import android.widget.Chronometer; import android.widget.DatePicker; import android.widget.TimePicker; import android.widget.Toast; import java.util.Calendar; public class MainActivity extends Activity { int year,month,day; DatePicker datePicker = null; TimePicker timePicker = null; Chronometer chronometer = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); datePicker = (DatePicker) findViewById(R.id.datepicker); Calendar calendar = Calendar.getInstance(); year = calendar.get(Calendar.YEAR); month = calendar.get(Calendar.MONTH); day = calendar.get(Calendar.DAY_OF_MONTH); //初始化日期选择器 datePicker.init(year, month, day, new DatePicker.OnDateChangedListener() { @Override public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) { MainActivity.this.year=year; MainActivity.this.month=day; MainActivity.this.day=day; show(year,month,day); } }); //时钟 timePicker = (TimePicker)findViewById(R.id.timepicker); timePicker.setIs24HourView(true);//设置成24小时的 timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() { @Override public void onTimeChanged(TimePicker view, int hourOfDay, int minute) { String string = hourOfDay + "时" + minute + "分"; Toast.makeText(MainActivity.this,string, Toast.LENGTH_SHORT).show(); } }); //计时器 chronometer = (Chronometer) findViewById(R.id.chronometer); //设置起始时间,获取系统时间 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(); } } }); } private void show(int year,int month,int day){ String str = year+"年"+(month+1)+"月"+day+"日"; Toast.makeText(MainActivity.this,str, Toast.LENGTH_SHORT).show(); } }
呈现界面