复选框,计时器,时间、日期选择器,

复选框

 
public class MainActivity extends AppCompatActivity {
    CheckBox CB1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);     
        CB1=findViewById(R.id.cb1);
        CB1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(CB1.isChecked()){
                    Toast.makeText(MainActivity.this,CB1.getText(),Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}

计时器


image.png
 
Chronometer chronometer;
        chronometer=findViewById(R.id.chr);
        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()>=20000){
                    Toast.makeText(MainActivity.this,"计时结束",Toast.LENGTH_LONG).show();
                    chronometer.stop();
                }
            }
        });

时间选择器

 
    
 TimePicker timePicker;
        timePicker=findViewById(R.id.tp);

        timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
            @Override
            public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
                String str=hourOfDay+"时"+minute+"分";
                Toast.makeText(MainActivity.this,str,Toast.LENGTH_SHORT).show();
            }
        });

日期选择器



    
datePicker=findViewById(R.id.dp);
        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=monthOfYear;
                MainActivity.this.day=dayOfMonth;
                show(year,monthOfYear,dayOfMonth);
            }
        });
           }
           public void show(int year,int month,int day){
            String str=year+"年"+(month+1)+"月"+day+"日";
            Toast.makeText(MainActivity.this,str,Toast.LENGTH_LONG).show();
    }

你可能感兴趣的:(复选框,计时器,时间、日期选择器,)