• 【基础篇】DatePickerDialog日期控件的基本使用(二) ——分别获取年、月、日、时、分


    项目步骤:

    1.在Main.xml布局文件中定义对应的组件,Main.xml内容如下:

    <?xml version="1.0" encoding="utf-8"?>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   

      android:layout_width="fill_parent"   

      android:layout_height="fill_parent"    

    android:orientation="vertical" >  

    <!-- 日期控件布局 -->   

      <LinearLayout        

    android:layout_width="fill_parent"  

    android:layout_height="wrap_content"        

    android:orientation="horizontal" >

    <EditText            

    android:id="@+id/showdate"            

    android:layout_width="fill_parent"            

    android:layout_height="wrap_content"            

    android:layout_weight="1" />

    <Button            

    android:id="@+id/pickdate"            

    android:layout_width="wrap_content"            

    android:layout_height="wrap_content"            

    android:text="选择日期" />    

    </LinearLayout>  

    <!-- 时间控件布局 -->    

    <LinearLayout        

    android:layout_width="fill_parent"        

    android:layout_height="wrap_content"       

    android:orientation="horizontal" >

    <EditText            

    android:id="@+id/showtime"            

    android:layout_width="fill_parent"            

    android:layout_height="wrap_content"            

    android:layout_weight="1" />

    <Button            

    android:id="@+id/picktime"            

    android:layout_width="wrap_content"            

    android:layout_height="wrap_content"            

    android:text="选择时间" />    

    </LinearLayout>

    </LinearLayout>

    2.在Activity中声明日期、时间控件,完成对其操作,Activity内容如下:

    public class MainActivity extends Activity {

     //声明变量,获取组件  

    private EditText showDate = null;

     private Button pickDate = null;

     private EditText showTime = null;  

    private Button pickTime = null;  

    //声明变量,用于对日期、时间控件的操作  

    private static final int SHOW_DATAPICK = 0;

     private static final int DATE_DIALOG_ID = 1;  

    private static final int SHOW_TIMEPICK = 2;  

    private static final int TIME_DIALOG_ID = 3;

     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.main);

      // 初始化控件和UI视图  

     initializeViews();   

    //新建一个Calendar对象获取Calendar信息   

    final 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);   

    //设置日期   

    setDateTime();   

    //设置时间   

    setTimeOfDay();  

    }

     /**   * 初始化控件和UI视图   */

     private void initializeViews() {  

     //根据id在布局文件中找到对应的组件

      showDate = (EditText) findViewById(R.id.showdate);   

    pickDate = (Button) findViewById(R.id.pickdate);   

    showTime = (EditText) findViewById(R.id.showtime);  

     pickTime = (Button) findViewById(R.id.picktime);   

    //为pickDate设置监听器,点击显示日期控件   

    pickDate.setOnClickListener(new View.OnClickListener() {

       @Override   

     public void onClick(View v) {

        //新建一个Message对象    

     Message msg = new Message();     

    if (pickDate.equals((Button) v)) {      

    msg.what = MainActivity.SHOW_DATAPICK;    

     }     

    //发出消息     

    MainActivity.this.dateandtimeHandler.sendMessage(msg);   

     }   

    });   

    //为pickTime设置监听器,点击显示时间控件  

     pickTime.setOnClickListener(new View.OnClickListener() {    

    @Override    

    public void onClick(View v) {     

    Message msg = new Message();     

    if (pickTime.equals((Button) v)) {      

    msg.what = MainActivity.SHOW_TIMEPICK;     

    }     

    //发出消息    

     MainActivity.this.dateandtimeHandler.sendMessage(msg);    

    }   

    });  

    }

     /**   * 设置日期   */  

    private void setDateTime() {  

     final Calendar c = Calendar.getInstance();   

    mYear = c.get(Calendar.YEAR);   

    mMonth = c.get(Calendar.MONTH);  

     mDay = c.get(Calendar.DAY_OF_MONTH);  

     //更新日期   

    updateDateDisplay();

     }

     /**   * 更新日期显示   */

     private void updateDateDisplay() {   

    showDate.setText(new StringBuilder().append(mYear).append("-")     .append((mMonth + 1) < 10 ? "0" + (mMonth + 1) : (mMonth + 1))     .append("-").append((mDay < 10) ? "0" + mDay : mDay));  

    }

     /**   * 日期控件的事件   */

     private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {

      public void onDateSet(DatePicker view, int year, int monthOfYear,     int dayOfMonth) {

       mYear = year;    mMonth = monthOfYear;    mDay = dayOfMonth;    

    //更新日期    

    updateDateDisplay();   

    }  

    };

     /**   * 设置时间   */

     private void setTimeOfDay() {   

    final Calendar c = Calendar.getInstance();   

    mHour = c.get(Calendar.HOUR_OF_DAY);  

     mMinute = c.get(Calendar.MINUTE);   

    //更新日期   

    updateTimeDisplay();  

    }

     /**   * 更新时间显示   */

     private void updateTimeDisplay() {   

    showTime.setText(new StringBuilder().append(mHour).append(":")     .append((mMinute < 10) ? "0" + mMinute : mMinute));

     }

     /**   * 时间控件事件   */  

    private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {   

    @Override   

    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {    

    mHour = hourOfDay;    

    mMinute = minute;    

    //更新时间    

    updateTimeDisplay();  

     }  

    };  

    /**   * 判断用户选择并作出响应   */  

    @Override  

    protected Dialog onCreateDialog(int id) {   

    switch (id) {   

    case DATE_DIALOG_ID:    return new DatePickerDialog(this, mDateSetListener, mYear, mMonth,      mDay);   

    case TIME_DIALOG_ID:    return new TimePickerDialog(this, mTimeSetListener, mHour, mMinute,      true);

      }   return null;

     }

     @Override  

    protected void onPrepareDialog(int id, Dialog dialog) {   

    switch (id) {  

     case DATE_DIALOG_ID:    ((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);    break;  

     case TIME_DIALOG_ID:    ((TimePickerDialog) dialog).updateTime(mHour, mMinute);    break;   

    }  

    }

     /**   * 处理日期和时间控件的Handler   */

     Handler dateandtimeHandler = new Handler() {  

     @Override  

     public void handleMessage(Message msg) {    

    switch (msg.what) {   

     case MainActivity.SHOW_DATAPICK:     

    //显示日期控件     

    showDialog(DATE_DIALOG_ID);     break;    

    case MainActivity.SHOW_TIMEPICK:     

    //显示时间控件     

    showDialog(TIME_DIALOG_ID);     break;   

     }  

     }

     };

    }

  • 相关阅读:
    两台电脑之间怎么用一根网线传输数据
    IDEA的几个常用配置,日常开发必备
    idea 配置Maven
    IDEA2019 安装和激活
    HTML中各类空格占位符
    jackSon注解-- @JsonInclude 注解不返回null值字段
    Vscode配置Eslint
    Tomcat中容器是什么以及容器与容器之间的数量关系。
    请求是如何传递给StandardEngine的?
    Java中一个线程只有六个状态。至于阻塞、可运行、挂起状态都是人们为了便于理解,自己加上去的。
  • 原文地址:https://www.cnblogs.com/tbcxy/p/3245251.html
Copyright © 2020-2023  润新知