(20120808)(01)android菜单与对话框--之日期及时间选择对话框
16lz
2021-12-04
(20120808)(01)android菜单与对话框--之日期及时间选择对话框
清单文件
- <?xml version="1.0" encoding="utf-8"?>
"http://schemas.android.com/apk/res/android" - package="com.ljq.dialog"
- android:versionCode="1"
- android:versionName="1.0">
- "@drawable/icon" android:label="@string/app_name">
- ".AlertDialog"
- android:label="@string/app_name">
- "android.intent.action.MAIN" />
"android.intent.category.LAUNCHER" /> "7" /> "android.permission.WRITE_CALENDAR" />
main.xml布局文件
- <?xml version="1.0" encoding="utf-8"?>
"@+id/LinearLayout01" - android:layout_width="fill_parent" android:layout_height="fill_parent"
- android:orientation="vertical"
- xmlns:android="http://schemas.android.com/apk/res/android">
"@+id/et" - android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:editable="false"
- android:cursorVisible="false" />
- android:id="@+id/dateBtn"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" />
- android:id="@+id/timeBtn"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" />
- android:text="@+id/digitalClock"
- android:textSize="20dip"
- android:gravity="center"
- android:id="@+id/DigitalClock01"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" />
- android:id="@+id/analogClock"
- android:gravity="center"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" />
AlertActivity类
- package com.ljq.dialog;
- import java.util.Calendar;
- import android.app.Activity;
- import android.app.DatePickerDialog;
- import android.app.Dialog;
- import android.app.TimePickerDialog;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.DatePicker;
- import android.widget.EditText;
- import android.widget.TimePicker;
- public class AlertDialog extends Activity {
- private Button dateBtn = null;
- private Button timeBtn = null;
- private EditText et=null;
- private final static int DATE_DIALOG = 0;
- private final static int TIME_DIALOG = 1;
- private Calendar c = null;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- et=(EditText)findViewById(R.id.et);
- dateBtn = (Button) findViewById(R.id.dateBtn);
- timeBtn = (Button) findViewById(R.id.timeBtn);
- dateBtn.setOnClickListener(new View.OnClickListener(){
- public void onClick(View v) {
- showDialog(DATE_DIALOG);
- }
- });
- timeBtn.setOnClickListener(new View.OnClickListener(){
- public void onClick(View v) {
- showDialog(TIME_DIALOG);
- }
- });
- }
- /**
- * 创建日期及时间选择对话框
- */
- @Override
- protected Dialog onCreateDialog(int id) {
- Dialog dialog = null;
- switch (id) {
- case DATE_DIALOG:
- c = Calendar.getInstance();
- dialog = new DatePickerDialog(
- this,
- new DatePickerDialog.OnDateSetListener() {
- public void onDateSet(DatePicker dp, int year,int month, int dayOfMonth) {
- et.setText("您选择了:" + year + "年" + (month+1) + "月" + dayOfMonth + "日");
- }
- },
- c.get(Calendar.YEAR), // 传入年份
- c.get(Calendar.MONTH), // 传入月份
- c.get(Calendar.DAY_OF_MONTH) // 传入天数
- );
- break;
- case TIME_DIALOG:
- c=Calendar.getInstance();
- dialog=new TimePickerDialog(
- this,
- new TimePickerDialog.OnTimeSetListener(){
- public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
- et.setText("您选择了:"+hourOfDay+"时"+minute+"分");
- }
- },
- c.get(Calendar.HOUR_OF_DAY),
- c.get(Calendar.MINUTE),
- false
- );
- break;
- }
- return dialog;
- }
- }
更多相关文章
- Android(安卓)WebView中的JavaScript调用android方法
- 修改dialog的大小
- Android中实现日期时间选择器(DatePicker和TimePicker)
- Android(安卓)-- Dialog(AlertDialog)
- Android菜鸟日记14-对话框
- Android(安卓)webview注入自己的js代码(js传入function等其他参数
- android之计时器 chronometer
- Android自定义对话框(Custom Dialog)
- Androidの自定义对话框AlertDialog(一)