国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android之日期及時間選擇對話框用法實例分析

2020-04-11 11:26:16
字體:
來源:轉載
供稿:網友

本文實例講述了Android之日期及時間選擇對話框用法。分享給大家供大家參考。具體如下:

清單文件:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.ljq.dialog" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".AlertDialog"   android:label="@string/app_name">  <intent-filter>  <action android:name="android.intent.action.MAIN" />  <category android:name="android.intent.category.LAUNCHER" />  </intent-filter> </activity> </application> <uses-sdk android:minSdkVersion="7" /> <uses-permission android:name="android.permission.WRITE_CALENDAR" /></manifest> 

main.xml布局文件:

<?xml version="1.0" encoding="utf-8"?><LinearLayout android:id="@+id/LinearLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"> <EditText android:id="@+id/et"  android:layout_width="fill_parent" android:layout_height="wrap_content"  android:editable="false" android:cursorVisible="false" /> <Button android:text="日期對話框"  android:id="@+id/dateBtn" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:text="時間對話框"  android:id="@+id/timeBtn" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <DigitalClock  android:text="@+id/digitalClock" android:textSize="20dip"  android:gravity="center" android:id="@+id/DigitalClock01"  android:layout_width="fill_parent" android:layout_height="wrap_content" /> <AnalogClock  android:id="@+id/analogClock" android:gravity="center"  android:layout_width="fill_parent" android:layout_height="wrap_content" /></LinearLayout>

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程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 九江县| 博白县| 左贡县| 肥东县| 涟水县| 大埔县| 麻江县| 达州市| 平塘县| 邹城市| 井陉县| 庄浪县| 定安县| 新沂市| 靖宇县| 万宁市| 哈密市| 安仁县| 东乡族自治县| 龙游县| 盐源县| 天水市| 长汀县| 东海县| 胶南市| 绍兴县| 屏东市| 永平县| 台南市| 宜州市| 龙南县| 信宜市| 南雄市| 安化县| 若尔盖县| 门头沟区| 施秉县| 峡江县| 崇阳县| 磐安县| 商丘市|