码迷,mamicode.com
首页 > 移动开发 > 详细

Android基础——基础UI控件

时间:2020-01-24 12:05:26      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:view   cte   alt   appcompat   字符串   eal   mep   ase   mic   

日历,时钟,计时器

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();
    }
}

呈现界面

技术图片

 

Android基础——基础UI控件

标签:view   cte   alt   appcompat   字符串   eal   mep   ase   mic   

原文地址:https://www.cnblogs.com/zsben991126/p/12231988.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!