Android基础——基础UI控件

Posted zsbenn

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android基础——基础UI控件相关的知识,希望对你有一定的参考价值。

日历,时钟,计时器

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控件的主要内容,如果未能解决你的问题,请参考以下文章

Android基础——基本UI控件:

Android 学习之路--android基础

Android自定义控件系列之基础篇

Android UI 基础知识

SwiftUI 基础控件-Gradient

android ui控件都有哪些