zoukankan      html  css  js  c++  java
  • 2.4.3 日期/时间选择器

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/layoutMain"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity" >
    
        <DatePicker
            android:id="@+id/date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal" />
    
        <TimePicker
            android:id="@+id/time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal" />
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:textSize="10pt"
            android:text="TextView" />
    
    </LinearLayout>
    package com.moon.hello2;
    
    import java.util.Calendar;
    
    import android.R.anim;
    import android.R.integer;
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.widget.ArrayAdapter;
    import android.widget.AutoCompleteTextView;
    import android.widget.DatePicker;
    import android.widget.DatePicker.OnDateChangedListener;
    import android.widget.TextView;
    import android.widget.TimePicker;
    import android.widget.TimePicker.OnTimeChangedListener;
    
    public class MainActivity extends Activity {
        private int year;
        private int month;
        private int day;
        private int hour;
        private int minute;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            this.setContentView(R.layout.activity_main);
            DatePicker datePicker = (DatePicker)findViewById(R.id.date);
            TimePicker timePicker = (TimePicker)findViewById(R.id.time);
            Calendar calendar = Calendar.getInstance();
            datePicker.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH),new OnDateChangedListener(){
    
                @Override
                public void onDateChanged(DatePicker date, int year, int month,
                        int day) {
                    // TODO Auto-generated method stub
                    MainActivity.this.year = year;
                    MainActivity.this.month = month;
                    MainActivity.this.day = day;
                    showDate();
                }
                
            });
            timePicker.setOnTimeChangedListener(new OnTimeChangedListener() {
                
                @Override
                public void onTimeChanged(TimePicker arg0, int hour, int minute) {
                    // TODO Auto-generated method stub
                    MainActivity.this.hour = hour;
                    MainActivity.this.minute = minute;
                    showDate();
                }
            });
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
        
        private void showDate(){
            TextView textView = (TextView)findViewById(R.id.textView1);
            textView.setText(year+"年"+month+"月"+day+"日"+hour+"小时"+minute+"分");
        }
    
    }
  • 相关阅读:
    java如何操作注册表(Preferences类)(在windows的注册表中保存、读取)
    转-正则表达式
    js数字格式化千分位格式
    es6严格模式需要注意的地方
    html5手势原理知识
    js事件总结
    js键盘相关知识总结
    html5 拖放
    js学习日记-隐式转换相关的坑及知识
    移动端各种分辨率匹配
  • 原文地址:https://www.cnblogs.com/Moon-Face/p/4503857.html
Copyright © 2011-2022 走看看