• Android 开发笔记___DatePicker__日期选择器


    虽然EditText提供了inputTtype=“date”,但用户往往不太喜欢自己输入时间。

      Android为这个提供了DatePicker,但有很多缺点,不是弹窗模式,而是直接在页面上占据一块区域,并且不会自动关闭。

        因此它不适合直接使用,在开发中往往用已经封装好的日期选择对话框DatePickerDialog。    

    • 相当于在AlertDialog上加载了DatePicker
    • 由监听器OnDateSetListener负责响应
    • 在OnDateSet方法中获得用户选择的具体日期
    • 当然,一月份对应的不是1,是0,你懂的!
     1 package com.example.alimjan.hello_world;
     2 
     3 import java.util.Calendar;
     4 
     5 /**
     6  * Created by alimjan on 7/15/2017.
     7  */
     8         import android.app.DatePickerDialog;
     9         import android.app.DatePickerDialog.OnDateSetListener;
    10 import android.content.Context;
    11 import android.content.Intent;
    12 import android.os.Bundle;
    13         import android.support.v7.app.AppCompatActivity;
    14         import android.view.View;
    15         import android.view.View.OnClickListener;
    16         import android.widget.DatePicker;
    17         import android.widget.TextView;
    18 
    19 import com.example.alimjan.hello_world.two.class__2_1_2;
    20 
    21 public class class_5_1_1 extends AppCompatActivity implements
    22         OnClickListener, OnDateSetListener {
    23 
    24     private TextView tv_date;
    25 
    26     @Override
    27     protected void onCreate(Bundle savedInstanceState) {
    28         super.onCreate(savedInstanceState);
    29         setContentView(R.layout.code_5_1_1);
    30         tv_date = (TextView) findViewById(R.id.tv_date);
    31         findViewById(R.id.btn_date).setOnClickListener(this);
    32     }
    33 
    34     @Override
    35     public void onClick(View v) {
    36         if (v.getId() == R.id.btn_date) {
    37             Calendar calendar = Calendar.getInstance();
    38             DatePickerDialog dialog = new DatePickerDialog(this, this,
    39                     calendar.get(Calendar.YEAR),
    40                     calendar.get(Calendar.MONTH),
    41                     calendar.get(Calendar.DAY_OF_MONTH));
    42             dialog.show();
    43         }
    44     }
    45 
    46     @Override
    47     public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
    48         String desc = String.format("您选择的日期是%d年%d月%d日",
    49                 year, monthOfYear+1, dayOfMonth);
    50         tv_date.setText(desc);
    51     }
    52 
    53     public static void startHome(Context mContext) {
    54         Intent intent = new Intent(mContext, class_5_1_1.class);
    55         mContext.startActivity(intent);
    56     }
    57 
    58 }
     1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     android:layout_width="match_parent"
     3     android:layout_height="match_parent"
     4     android:orientation="vertical"
     5     android:padding="10dp" >
     6 
     7     <Button
     8         android:id="@+id/btn_date"
     9         android:layout_width="match_parent"
    10         android:layout_height="wrap_content"
    11         android:text="请选择日期"
    12         android:textColor="@color/black"
    13         android:textSize="20sp" />
    14 
    15     <TextView
    16         android:id="@+id/tv_date"
    17         android:layout_width="match_parent"
    18         android:layout_height="wrap_content"
    19         android:textColor="@color/black"
    20         android:textSize="17sp" />
    21 
    22 </LinearLayout>
  • 相关阅读:
    Eclipse生成部署描述符(web.xml)
    异步Servlet和异步过虑器
    安装 R 及 R 包
    Servlet封装类
    设计模式——装饰者模式
    Eclipse快速生成覆盖方法、Getter、Setter的方法
    查看CPU核数和内存
    Filter 过滤器
    Listener 监听器
    Tag file
  • 原文地址:https://www.cnblogs.com/alimjan/p/7182277.html
Copyright © 2020-2023  润新知