美文网首页
复选框,计时器,时间、日期选择器,

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

作者: 被罚站的树 | 来源:发表于2019-05-16 21:01 被阅读0次

    复选框

     <CheckBox
            android:id="@+id/cb1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Football"
            android:checked="true"
            />
    
    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
            android:id="@+id/chr"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:format="过去时间%s"/>
    
    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
            android:id="@+id/tp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        </TimePicker>
    
     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
            android:id="@+id/dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
    
        </DatePicker>
    
    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();
        }
    

    相关文章

      网友评论

          本文标题:复选框,计时器,时间、日期选择器,

          本文链接:https://www.haomeiwen.com/subject/gwluaqtx.html