zoukankan      html  css  js  c++  java
  • Android DatePickerDialog TimePickerDialog

     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 
     6     <Button
     7         android:layout_width="match_parent"
     8         android:layout_height="wrap_content"
     9         android:onClick="datePickerDialog"
    10         android:text="DatePickerDialog" />
    11     
    12     <Button
    13         android:layout_width="match_parent"
    14         android:layout_height="wrap_content"
    15         android:onClick="timePickerDialog"
    16         android:text="TimePickerDialog" />
    17 
    18 </LinearLayout>
    activity_main.xml
     1 public class MainActivity extends Activity {
     2 
     3     @Override
     4     protected void onCreate(Bundle savedInstanceState) {
     5         super.onCreate(savedInstanceState);
     6         setContentView(R.layout.activity_main);
     7     }
     8 
     9     /**
    10      * DatePickerDialog和TimePickerDialog都继承于AlertDialog,基本方法和DataPicker和TimePicker组件一样,
    11      * 只需要调用Dialog的show()方法来显示,在构造方法里传入回调监听
    12      * @param v
    13      */
    14     int year = Calendar.getInstance().get(Calendar.YEAR);
    15     int month = Calendar.getInstance().get(Calendar.MONTH);
    16     int dayOfMonth = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
    17     public void datePickerDialog(View v){
    18         DatePickerDialog dialog = new DatePickerDialog(MainActivity.this, new OnDateSetListener() {
    19             @Override
    20             public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
    21                 Toast.makeText(MainActivity.this, year+"-"+(month+1)+"-"+dayOfMonth, Toast.LENGTH_SHORT).show();
    22                 
    23             }
    24         }, year, month, dayOfMonth);
    25         dialog.show();
    26     }
    27     
    28     int hourOfDay = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
    29     int minute = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
    30     public void timePickerDialog(View v){
    31         TimePickerDialog dialog = new TimePickerDialog(MainActivity.this, new OnTimeSetListener() {
    32             
    33             @Override
    34             public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    35                 Toast.makeText(MainActivity.this, hourOfDay+":"+minute, Toast.LENGTH_SHORT).show();
    36             }
    37         }, hourOfDay, minute, true);
    38         dialog.show();
    39     }
    40 }
    MainActivity.java
  • 相关阅读:
    Androidproject文件下assets目录与res目录的差别
    tomcat 实现域名crm.test.com訪问
    全文索引--自己定义chinese_lexer词典
    Elasticsearch 之 慘痛部署(分片移位)
    iOS开发核心语言Objective C —— 全部知识点总结
    [LeedCode OJ]#28 Implement strStr()
    【Django】 gunicorn部署纪要
    PKI(公钥基础设施)基础知识笔记
    Cocoapods Undefined symbols for architecture armv7sarm64
    Android开发Tips(3)
  • 原文地址:https://www.cnblogs.com/Claire6649/p/5969003.html
Copyright © 2011-2022 走看看