首页 > 代码库 > 安卓学习第37课——DatePickerDialog、TimePickerDialog
安卓学习第37课——DatePickerDialog、TimePickerDialog
<?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" android:gravity="center_horizontal" ><EditText android:id="@+id/show" android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="none" /><LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" ><Button android:id="@+id/dateBn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="设置日期" /><Button android:id="@+id/timeBn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="设置时间" /> </LinearLayout></LinearLayout>
package com.example.datetimedialog;import java.util.Calendar;import android.app.Activity;import android.app.DatePickerDialog;import android.app.TimePickerDialog;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.DatePicker;import android.widget.EditText;import android.widget.TimePicker;public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button dateBn=(Button) findViewById(R.id.dateBn); Button timeBn=(Button) findViewById(R.id.timeBn); dateBn.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { Calendar c=Calendar.getInstance(); new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { EditText show=(EditText) findViewById(R.id.show); show.setText("您选择了"+year+"年"+(monthOfYear+1)+"月"+dayOfMonth+"日"); } } ,c.get(Calendar.YEAR) , c.get(Calendar.MONTH) , c.get(Calendar.DAY_OF_MONTH)).show(); } }); timeBn.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { Calendar c=Calendar.getInstance(); new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { EditText show=(EditText) findViewById(R.id.show); show.setText("您选择了"+hourOfDay+"时"+minute+"分"); } } ,c.get(Calendar.HOUR_OF_DAY) , c.get(Calendar.MINUTE) , true).show(); } }); }}
安卓学习第37课——DatePickerDialog、TimePickerDialog
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。