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+"分");
        }
    
    }
  • 相关阅读:
    JS小技巧总汇
    [转贴]聪明人如何拯救你的职业困
    Button按钮多行显示的实现方法
    事件和委托
    支持~
    关于递归
    Android 资源的国际化
    Android 文件的浏览(类似于FileDialog的功能)
    Android 开发TCP、UdP客户端
    Android 为什么现在google不让结束整个程序,只让结束单个Activity(转)
  • 原文地址:https://www.cnblogs.com/Moon-Face/p/4503857.html
Copyright © 2011-2022 走看看