1 package com.example.android_dialog;
2
3 import java.util.Calendar;
4
5 import android.app.Activity;
6 import android.app.AlertDialog;
7 import android.app.DatePickerDialog;
8 import android.app.TimePickerDialog;
9 import android.os.Bundle;
10 import android.view.Gravity;
11 import android.view.LayoutInflater;
12 import android.view.View;
13 import android.view.View.OnClickListener;
14 import android.widget.Button;
15 import android.widget.DatePicker;
16 import android.widget.EditText;
17 import android.widget.PopupWindow;
18 import android.widget.TimePicker;
19 import android.widget.Toast;
20
21 /**
22 * Dialog的几种用法
23 *
24 * @author Administrator
25 *
26 */
27 public class MainActivity extends Activity implements OnClickListener {
28 TimePickerDialog.OnTimeSetListener timeSetListener;
29 DatePickerDialog.OnDateSetListener dateSetListener;
30
31 @Override
32 protected void onCreate(Bundle savedInstanceState) {
33 super.onCreate(savedInstanceState);
34 setContentView(R.layout.activity_main);
35
36 Button button = (Button) findViewById(R.id.btn);
37 button.setOnClickListener(this);
38
39 Button button1 = (Button) findViewById(R.id.btn1);
40 button1.setOnClickListener(this);
41 Button button2 = (Button) findViewById(R.id.btn2);
42 button2.setOnClickListener(this);
43
44 Button button3 = (Button) findViewById(R.id.btn3);
45 button3.setOnClickListener(this);
46 // 日期Dialog的监听类
47 dateSetListener = new DatePickerDialog.OnDateSetListener() {
48
49 @Override
50 public void onDateSet(DatePicker view, int year, int monthOfYear,
51 int dayOfMonth) {
52 Toast.makeText(getApplicationContext(),
53 year + "--" + monthOfYear + "--" + dayOfMonth,
54 Toast.LENGTH_SHORT).show();
55
56 }
57 };
58 // 时间Dialog的监听类
59 timeSetListener = new TimePickerDialog.OnTimeSetListener() {
60
61 @Override
62 public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
63 Toast.makeText(getApplicationContext(),
64 hourOfDay + ":" + minute, Toast.LENGTH_SHORT).show();
65 }
66 };
67 }
68
69 @Override
70 public void onClick(View v) {
71 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
72 Calendar calendar = Calendar.getInstance();
73
74 final AlertDialog dialog;
75 switch (v.getId()) {
76 // 普通Dialog界面
77 case R.id.btn:
78 dialog = builder.create();
79 dialog.setTitle("提醒");
80 dialog.setIcon(R.drawable.ic_launcher);
81 dialog.setMessage("登录失败");
82 dialog.show();
83 break;
84 // 自定Dialog自定义界面
85 case R.id.btn1: {
86 dialog = builder.create();
87 dialog.setTitle("提醒");
88 dialog.setIcon(R.drawable.ic_launcher);
89 // 加载界面
90 LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
91 View view = inflater.inflate(R.layout.dialogtest, null);
92 final EditText editText = (EditText) view
93 .findViewById(R.id.editText1);
94 final EditText editText2 = (EditText) view
95 .findViewById(R.id.editText2);
96 Button button = (Button) view.findViewById(R.id.okbtn);
97 button.setOnClickListener(new OnClickListener() {
98
99 @Override
100 public void onClick(View v) {
101 Toast.makeText(
102 getApplicationContext(),
103 editText.getText().toString() + "-----"
104 + editText2.getText().toString(),
105 Toast.LENGTH_SHORT).show();
106 dialog.dismiss();
107 }
108 });
109 // dialog加载界面
110 dialog.setView(view);
111 dialog.show();
112 }
113 break;
114 case R.id.btn2:
115 // 日期Dialog
116 DatePickerDialog datePickerDialog = new DatePickerDialog(
117 MainActivity.this, dateSetListener,
118 calendar.get(calendar.YEAR), calendar.get(calendar.MONTH),
119 calendar.get(calendar.DAY_OF_MONTH));
120 // 时间Dialog
121 TimePickerDialog timePickerDialog = new TimePickerDialog(
122 MainActivity.this, timeSetListener,
123 calendar.get(calendar.HOUR_OF_DAY),
124 calendar.get(calendar.MINUTE), true);
125 datePickerDialog.show();
126
127 break;
128 case R.id.btn3: {
129 // PopupWindow
130 LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
131 View view = inflater.inflate(R.layout.dialogtest, null);
132 final PopupWindow popupWindow = new PopupWindow(view, 320, 320);
133 Button button = (Button) view.findViewById(R.id.okbtn);
134 button.setOnClickListener(new OnClickListener() {
135
136 @Override
137 public void onClick(View v) {
138 // popupWindow消失
139 popupWindow.dismiss();
140
141 }
142 });
143 // 在屏幕底部显示
144 popupWindow.showAtLocation(v, Gravity.BOTTOM, 0, 0);
145 }
146 break;
147 }
148
149 }
150 }