Android 开发中DatePicker、TimePicker

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/yezhouxiang/article/details/70173351
  • DatePicker的使用
  • TimePicker的使用
  • 关于匿名内部类

XML文件中对控件的布局

<TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请选择时间:" />
    <DatePicker
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity = "center_vertical"
        android:id="@+id/datePicker"
        android:layout_below="@+id/textView">
    </DatePicker>
    <TimePicker
        android:id = "@+id/timePicker"
        android:layout_gravity = "center_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/datePicker">
    </TimePicker>
    <EditText
        android:id="@+id/show"
        android:cursorVisible="false"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/timePicker"/>

因为这只是一个简单的Demo,所以只是简单的用了几个控件来作个展示使用

java中的代码

public class MainActivity extends Activity {
    private DatePicker datePicker;
    private TimePicker timePicker;
    private EditText mEditText;
    // 定义5个记录当前时间的变量
    private int year;
    private int month;
    private int day;
    private int hour;
    private int minute;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }
    private  void initView(){
        datePicker = (DatePicker)findViewById(R.id.datePicker);
        timePicker = (TimePicker)findViewById(R.id.timePicker);
        mEditText = (EditText)findViewById(R.id.show);
        // 获取当前的时间

       // Calendar ca = Calendar.getInstance();
        Calendar ca = Calendar.getInstance();

        year = ca.get(Calendar.YEAR);
        month = ca.get(Calendar.MONTH);
        day = ca.get(Calendar.DAY_OF_MONTH);
        hour = ca.get(Calendar.HOUR);
        minute = ca.get(Calendar.MINUTE);
        Date d = new Date();
       d.setTime(System.currentTimeMillis());


        // 初始化DatePicker控件
        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 = monthOfYear;
                MainActivity.this.day = dayOfMonth;
                // 显示当前时间
                showDate(year,month,day,hour,minute);
            }
        });
        timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
            @Override
            public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
                MainActivity.this.hour = hourOfDay;
                MainActivity.this.minute = minute;
                showDate(year,month,day,hour,minute);
            }
        });
    }

    // 显示当前时间
    private void showDate(int year,int month,int day,int hour,int minute){
        mEditText.setText("日期为:"+ year+"年"+(month+1)+"月"+day+"日"+hour+"时"+minute+"分");
    }
}

这边为什么要在month上加一个1,因为在Android开发中月份是从0开始的,所以在加上一个1就能够匹配上了。

展开阅读全文

没有更多推荐了,返回首页