package com.zte.android.lession; import java.util.Calendar; import android.app.Activity; import android.app.AlertDialog; import android.app.DatePickerDialog; import android.app.DatePickerDialog.OnDateSetListener; import android.app.ProgressDialog; import android.app.TimePickerDialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.DatePicker; import android.widget.TimePicker; public class Activity_010_Dialog extends Activity { private Button alertBtn01 , alertBtn02 , progressBtn , datepickerBtn , timepickerBtn ; /** Called when the activity is first created. * 分别展示alertDialog、progressDialog、 * datepickerdialog、timepickerdialog */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_010_dialog_layout); //初始化button alertBtn01 = (Button)findViewById(R.id.l010_button1); alertBtn02 = (Button)findViewById(R.id.l010_button2); progressBtn = (Button)findViewById(R.id.l010_button3); datepickerBtn = (Button)findViewById(R.id.l010_button4); timepickerBtn = (Button)findViewById(R.id.l010_button5); //设置监听器 alertBtn01.setOnClickListener(alertBtn01Listener); alertBtn02.setOnClickListener(alertBtn02Listener); progressBtn.setOnClickListener(progressBtnListener); datepickerBtn.setOnClickListener(datepickerBtnListener); timepickerBtn.setOnClickListener(timepickerBtnListener); } //alertDialog_01 private OnClickListener alertBtn01Listener = new OnClickListener() { public void onClick(View v) { //创建AlertDialog.Builder AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Activity_010_Dialog.this); //通过alertDialogBuilder初始化alertdialog alertDialogBuilder.setIcon(R.drawable.ic_launcher); alertDialogBuilder.setTitle("alertDialogTitle01"); alertDialogBuilder.setMessage("alertDialogMessage01"); //初始化 取消、是、否 — 按钮 alertDialogBuilder.setNegativeButton("negative_no", closeDialogListener); alertDialogBuilder.setPositiveButton("positive_yes", closeDialogListener); alertDialogBuilder.setNeutralButton("neutral_canncel", closeDialogListener); //create - show alertDialogBuilder.create(); alertDialogBuilder.show(); } }; //alertDialog_02 private OnClickListener alertBtn02Listener = new OnClickListener() { public void onClick(View v) { //创建AlertDialog.Builder AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Activity_010_Dialog.this); //通过alertDialogBuilder获取alertdialog AlertDialog alertDialog = alertDialogBuilder.create(); alertDialog.setIcon(R.drawable.ic_launcher); alertDialog.setTitle("alertDialogTitle02"); alertDialog.setMessage("alertDialogMessage02"); //初始化 取消、是、否 — 按钮 alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE,"NEGATIVE", closeDialogListener); alertDialog.setButton(AlertDialog.BUTTON_POSITIVE,"POSITIVE", closeDialogListener); alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL,"NEUTRAL", closeDialogListener); //show alertDialog.show(); } }; //closeDialogListener final DialogInterface.OnClickListener closeDialogListener = new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub dialog.cancel(); } }; //progressBtnListener private OnClickListener progressBtnListener = new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub ProgressDialog progressDialog = new ProgressDialog(Activity_010_Dialog.this); progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressDialog.setMax(100); progressDialog.setTitle("progressDialogTitle"); progressDialog.setMessage("progress dialog message ... "); progressDialog.setButton(ProgressDialog.BUTTON_NEGATIVE,"NEGATIVE", closeDialogListener); progressDialog.setButton(ProgressDialog.BUTTON_POSITIVE,"POSITIVE", closeDialogListener); progressDialog.setButton(ProgressDialog.BUTTON_NEUTRAL,"NEUTRAL", closeDialogListener); progressDialog.show(); progressDialog.setProgress(50); } }; //datepickerBtnListener private OnClickListener datepickerBtnListener = new OnClickListener() { public void onClick(View v) { Calendar c = Calendar.getInstance(); DatePickerDialog datePickerDialog = new DatePickerDialog(Activity_010_Dialog.this,datePickerDialogCallBack, c.get(Calendar.YEAR), c.get(Calendar.MONDAY),c.get(Calendar.DATE)); datePickerDialog.setTitle("datepickerTitle"); datePickerDialog.setMessage("datePickerDialog_message"); datePickerDialog.setButton(DatePickerDialog.BUTTON_NEGATIVE,"NEGATIVE", closeDialogListener); datePickerDialog.setButton(DatePickerDialog.BUTTON_POSITIVE,"POSITIVE", closeDialogListener); datePickerDialog.setButton(DatePickerDialog.BUTTON_NEUTRAL,"NEUTRAL", closeDialogListener); datePickerDialog.show(); } }; final DatePickerDialog.OnDateSetListener datePickerDialogCallBack = new OnDateSetListener() { public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { // TODO Auto-generated method stub view.updateDate(year, monthOfYear+1, dayOfMonth); } }; //timepickerBtnListener private OnClickListener timepickerBtnListener = new OnClickListener() { public void onClick(View v) { Calendar c = Calendar.getInstance(); TimePickerDialog timePickerDialog = new TimePickerDialog(Activity_010_Dialog.this, timepickerBtnListenerCallBack, c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE), true); timePickerDialog.setTitle("timePickerDialogTitle"); timePickerDialog.setMessage("timePickerDialog_message"); timePickerDialog.setButton(TimePickerDialog.BUTTON_NEGATIVE,"NEGATIVE", closeDialogListener); timePickerDialog.setButton(TimePickerDialog.BUTTON_POSITIVE,"POSITIVE", closeDialogListener); timePickerDialog.setButton(TimePickerDialog.BUTTON_NEUTRAL,"NEUTRAL", closeDialogListener); timePickerDialog.show(); } }; final TimePickerDialog.OnTimeSetListener timepickerBtnListenerCallBack = new TimePickerDialog.OnTimeSetListener() { public void onTimeSet(TimePicker view, int hourOfDay, int minute) { // TODO Auto-generated method stub view.setCurrentHour(hourOfDay); view.setCurrentMinute(minute); } }; }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/l010_button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="警告对话框01" /> <Button android:id="@+id/l010_button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="警告对话框02" /> <Button android:id="@+id/l010_button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="进度对话框" /> <Button android:id="@+id/l010_button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="日期选择对话框" /> <Button android:id="@+id/l010_button5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="时间选择对话框" /> </LinearLayout>