扫一扫,加我们为好友有惊喜哦~
-->

核心课程
开班动态
专题服务

您当前所在位置:首页 > 中心动态 >>
如何制作安卓中的日期对话框—常州电脑培训

9、日期、时间选择对话框(DatePickerDialogTimePickerDialog

    A、调用Activity中的onCreateDialog方法创建对话框

    B、分别在OnDateSetListenerOnDateSet方法和OnTimeSetListeneronTimeSet事件方法中更改时间和日期

    C、调用ActivityshowDialog方法显示对话框

案例:

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

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

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

    <Button android:text="更新日期" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>

    <Button android:text="更新时间" android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>

    <TextView android:text="" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>

    <TextView android:text="" android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>

</LinearLayout>

 

MyDateActivity

    private Button button01, button02;

    private TextView tv01, tv02;

    private Calendar ca;

 

    private int month, year, day;

    private int hour, minute;

 

    @Override

    public void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       setContentView(R.layout.main);

 

       this.button01 = (Button) this.findViewById(R.id.button1);

       this.button02 = (Button) this.findViewById(R.id.button2);

 

       ca = Calendar.getInstance();

 

       month = ca.get(Calendar.MONTH);

       year = ca.get(Calendar.YEAR);

       day = ca.get(Calendar.DAY_OF_MONTH);

 

       hour = ca.get(Calendar.HOUR);

       minute = ca.get(Calendar.MINUTE);

 

       this.tv01 = (TextView) this.findViewById(R.id.textView1);

       this.tv02 = (TextView) this.findViewById(R.id.textView2);

 

       tv01.setText(year + " : " + month + " : " + day);

       tv02.setText(hour + " : " + minute);

 

       button01.setOnClickListener(new View.OnClickListener() {

 

           @Override

           public void onClick(View arg0) {

              // TODO Auto-generated method stub

              showDialog(0);

           }

       });

 

       button02.setOnClickListener(new View.OnClickListener() {

 

           @Override

           public void onClick(View arg0) {

              // TODO Auto-generated method stub

              showDialog(1);

           }

       });

    }

 

    @Override

    protected Dialog onCreateDialog(int id) {

       Dialog dialog = null;

       // 创建日期对话框

       if (id == 0) {

           dialog = new DatePickerDialog(this, listener1, year, month, day);

       } else if (id == 1) {

           dialog = new TimePickerDialog(this, listener2, hour, minute, false);

       }

 

       return dialog;

    }

 

    // 当时间被设定的时候

    private OnDateSetListener listener1 = new OnDateSetListener() {

 

       @Override

       public void onDateSet(DatePicker view, int y, int m, int d) {

           year = y;

           month = m;

           day = d;

           // textview重设日期

           tv01.setText(year + " : " + month + " : " + day);

       }

 

    };

 

    //当时间被设定的时候监听器启动

    private OnTimeSetListener listener2 = new OnTimeSetListener() {

 

       @Override

       public void onTimeSet(TimePicker view, int h, int m) {

           hour = h;

           minute = m;

           tv02.setText(hour + " : " + minute);

       }

    };

更多精彩,请关注:常州电脑培训常州软件培训



返回顶部