zoukankan      html  css  js  c++  java
  • Android学习笔记(四)时钟、时间

    AnalogClock实现时钟


      android.os.Handle、java.lang.Thread以及android.os.Message三对象的整合实现。

      通过生成一个Thread,在进程内调用System.currentTimeMillis()取得系统时间,并通过Message对象来通知Handler对象,Handler作为联系Activity与Thread之间的桥梁。

    package com.example.test15;
    
    import java.util.Calendar;
    
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.app.Activity;
    import android.view.Menu;
    import android.widget.AnalogClock;
    import android.widget.TextView;
    
    public class MainActivity extends Activity {
    
        protected static final int GUINOTIFIER=0x1234;
        
        private TextView mTextView;
        public AnalogClock mAnalogClock;
        
        public Calendar mCalendar;
        public int mMinutes;
        public int mHour;
        public int mSecond;
        
        public Handler mHandler;
        private Thread mClockThread;
        
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            mTextView=(TextView)findViewById(R.id.textView1);
            mAnalogClock=(AnalogClock)findViewById(R.id.analogClock1);
            
            //Handler来接收运行线程所需要传递的信息
            mHandler=new Handler(){
                public void handleMessage(Message msg){
                    switch(msg.what){
                    case MainActivity.GUINOTIFIER:
                        mTextView.setText(mHour+":"+mMinutes+":"+mSecond);
                        break;
                    }
                    super.handleMessage(msg);
                }
            };
            
            mClockThread = new LooperThread();
            mClockThread.start();
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.activity_main, menu);
            return true;
        }
    
        class LooperThread extends Thread{
            public void run(){
                super.run();
                try{
                    do{
                        long time = System.currentTimeMillis();
                        final Calendar mCalendar=Calendar.getInstance();
                        mCalendar.setTimeInMillis(time);
                        mHour=mCalendar.get(Calendar.HOUR);
                        mMinutes=mCalendar.get(Calendar.MINUTE);
                        mSecond=mCalendar.get(mCalendar.SECOND);
                        
                        //Thread.sleep(3000);
                        
                        Message m=new Message();
                        m.what=MainActivity.GUINOTIFIER;
                        MainActivity.this.mHandler.sendMessage(m);
                        
                    }while(MainActivity.LooperThread.interrupted()==false);
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
        }
    }

    DatePicker和TimePicker实现时间选择


      日期选择组件DatePicker 和 时间选择组件TimePicker。

      首先通过,Calendar c=Calendar.getInstance();获取系统时间和日期。

      再利用DatePicker.init()来更新日期,利用TimePicker.setOnTimeChangedListerner()来监听时间的修改(这里可以通过TimePicker.setIs24HourView来设置时间是否为24格式)。

    package com.example.test16;
    
    import java.text.Format;
    import java.util.Calendar;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.DatePicker;
    import android.widget.TextView;
    import android.widget.TimePicker;
    import android.support.v4.app.NavUtils;
    
    public class MainActivity extends Activity {
    
        private TextView mTextView;
        TimePicker mTimePicker;
        DatePicker mDatePicker;
        
        private int mYear;
        private int mMonth;
        private int mDay;
        private int mHour;
        private int mMinute;
        
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            getDateTime();
            mTextView=(TextView)findViewById(R.id.textView1);
            updateDisplay();
            
            mDatePicker=(DatePicker)findViewById(R.id.datePicker1);
            mDatePicker.init(mYear, mMonth, mDay, new DatePicker.OnDateChangedListener() {
                
                public void onDateChanged(DatePicker view, int year, int monthOfYear,
                        int dayOfMonth) {
                    // TODO Auto-generated method stub
                    mYear=year;
                    mMonth=monthOfYear;
                    mDay=dayOfMonth;
                    
                    updateDisplay();
                }
            });
            
            mTimePicker=(TimePicker)findViewById(R.id.timePicker1);
            mTimePicker.setIs24HourView(true);
            mTimePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
                
                public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
                    // TODO Auto-generated method stub
                    mHour=hourOfDay;
                    mMinute=minute;
                    
                    updateDisplay();
                }
            });
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.activity_main, menu);
            return true;
        }
    
        public void getDateTime(){
            Calendar c=Calendar.getInstance();
            mYear=c.get(Calendar.YEAR);
            mMonth=c.get(Calendar.MONTH);
            mDay=c.get(Calendar.DAY_OF_MONTH);
            mHour=c.get(Calendar.HOUR_OF_DAY);
            mMinute=c.get(Calendar.MINUTE);
        }
        
        private void updateDisplay(){
            mTextView.setText(
                    new StringBuilder().append(mYear).append("/").append(format(mMonth+1)).append("/").append(format(mDay)).append("  ")
                        .append(format(mHour)).append(":").append(format(mMinute))
                    );
        }
        //int to string
        private String format(int x){
            String s=""+x;
            if(s.length()==1)
                s="0"+s;
            return s;
        }
    }
  • 相关阅读:
    判断文件是否存在
    peewee模块更改数据-sqlite
    Python操纵数据库:peewee
    PyQt5调用主窗口
    FHQ-Treap学习笔记
    P3047 [USACO12FEB]Nearby Cows G(树形DP)
    P2986 [USACO10MAR]Great Cow Gathering G(树形DP)
    【普及】Codeforces Round #713(Div 3) 1512A~G
    P1272.重建道路(树形背包)
    P273.有线电视网(树上背包)
  • 原文地址:https://www.cnblogs.com/coder2012/p/3089792.html
Copyright © 2011-2022 走看看