zoukankan      html  css  js  c++  java
  • AnalogClock和DigitalClock时间和日期控件

       AnalogClock和DigitalClock(显示时钟的控件).

    一、建立工程,如图

    二、activity_main.xml中代码

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        
        <AnalogClock 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
                    
            />
        <DigitalClock 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="18dp"
            />
        
        <Button 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button1"
            android:text="显示TimePickerDialog"
            />
        <Button 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button2"
            android:text="显示DatePickerDialog"
            />
          
        
    </LinearLayout>
    View Code

    三、MainActivity.java中代码

    package com.study.analogcolck;
    
    import java.util.Calendar;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.app.DatePickerDialog;
    import android.app.DatePickerDialog.OnDateSetListener;
    import android.app.TimePickerDialog;
    import android.app.TimePickerDialog.OnTimeSetListener;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.DatePicker;
    import android.widget.TimePicker;
    import android.widget.Toast;
    
    public class MainActivity extends Activity implements OnClickListener {
    
        private Button button1,button2;
        private int hourOfDay,minute;
        private int year,monthOfYear,dayOfMonth;
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            button1 = (Button)this.findViewById(R.id.button1);
            button2 = (Button)this.findViewById(R.id.button2);
            button1.setOnClickListener(this);
            button2.setOnClickListener(this);
            
            //获得当前的时间,获得小时和分钟
            Calendar calendar = Calendar.getInstance();
            hourOfDay = calendar.get(Calendar.HOUR_OF_DAY);
            minute = calendar.get(Calendar.MINUTE); 
            year = calendar.get(Calendar.YEAR);
            monthOfYear = calendar.get(Calendar.MONTH);
            dayOfMonth = calendar.get(calendar.DAY_OF_MONTH);
        }
    
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    
    
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.button1:
                TimePickerDialog timePickerDialog = new TimePickerDialog(MainActivity.this, new MyTimePickerDialog(), hourOfDay, minute, true);
                timePickerDialog.show();
                break;
            case R.id.button2:
                DatePickerDialog datePickerDialog = new DatePickerDialog(MainActivity.this, new MyDatePickerDialog(), year, monthOfYear, dayOfMonth);
                datePickerDialog.show();
                break;
            default:
                break;
            }
            
        }
        
        public class MyTimePickerDialog implements OnTimeSetListener{
    
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                Toast.makeText(MainActivity.this, "H:" + hourOfDay +"   minute:" + minute, 1).show();
        
            }
            
        }
        public class MyDatePickerDialog implements OnDateSetListener{
    
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear,
                    int dayOfMonth) {
                Toast.makeText(MainActivity.this, year+"年" + monthOfYear+" 月" +dayOfMonth+ "  日", 1).show();
                
            }
            
        }
        
    }
    View Code

    四、效果图

  • 相关阅读:
    codevs 3657 括号序列
    洛谷P1962 斐波那契数列
    Black Rock shooter
    codevs 2596 售货员的难题
    51Nod-1154 回文串划分
    UVA
    POJ3321[苹果树] 树状数组/线段树 + dfs序
    Hdu 4578 Transformation (线段树 分类分析)
    786B
    438D
  • 原文地址:https://www.cnblogs.com/kingshow123/p/analogclock.html
Copyright © 2011-2022 走看看