public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } // 全部對話框記得要加.show()方法,不然不會顯示出對話框 // 普通對話框,(警告對話框 、提示對話框) public void alert(View view) { // 能夠在監聽裏面實現本身的一些功能 AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("提示"); builder.setIcon(R.drawable.ic_launcher); builder.setMessage("您真的要退出?"); builder.setNegativeButton("取消", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT) .show();// 沒有實現功能,只是點擊了提示一下 } }); builder.setPositiveButton("肯定", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "肯定", Toast.LENGTH_SHORT) .show(); } }); builder.setNeutralButton("中立", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "中立", Toast.LENGTH_SHORT) .show(); } }); builder.show(); } // 進度對話框 // 全部對話框記得要加.show()方法,不然不會顯示出對話框 public void progress(View view) { ProgressDialog dialog = new ProgressDialog(this); dialog.setTitle("提示"); dialog.setIcon(R.drawable.ic_launcher); dialog.setMessage("正在下載"); dialog.show();// 彈出一個對話框,顯示一個去圈圈在加載 } // 日期對話框,能夠選擇日期,當前日期 public void date(View view) { Calendar calendar = Calendar.getInstance();// 獲取當前時間 int year= calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); int day = calendar.get(Calendar.DAY_OF_MONTH); DatePickerDialog date = new DatePickerDialog(this, new OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { // 月要加一,由於是從0開始 String datetime = year + "-" + (monthOfYear + 1) + "-" + dayOfMonth; Toast.makeText(MainActivity.this, datetime, Toast.LENGTH_SHORT).show(); } }, year, month, day); date.show(); } // 時間對話框 public void time(View view) { Calendar calendar = Calendar.getInstance(); int hour = calendar.get(Calendar.HOUR_OF_DAY); int minute = calendar.get(Calendar.MILLISECOND); TimePickerDialog time = new TimePickerDialog(this, new OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { String time = hourOfDay + "-" + minute; Toast.makeText(MainActivity.this, time, Toast.LENGTH_SHORT).show(); } }, hour, minute, true); time.show(); } // 自定義對話框,對話框中能夠存放本身的佈局控件 public void custom(View view) { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("用戶登陸"); builder.setIcon(R.drawable.ic_launcher); // 填充自定義的登陸視圖 View loginView = LayoutInflater.from(this).inflate(R.layout.item, null); final TextView txet_name = (TextView) loginView .findViewById(R.id.text_name); final TextView txet_pass = (TextView) loginView .findViewById(R.id.text_pass); builder.setView(loginView); // 添加一個肯定按鈕 builder.setPositiveButton("肯定", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String name = txet_name.getText().toString(); String pass = txet_pass.getText().toString(); setTitle(name + ":" + pass); } }); builder.setNegativeButton("取消", null); builder.show(); } } 自定義對話框的自定義佈局 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <EditText android:id="@+id/text_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="請輸入用戶名" android:inputType="textPersonName" /> <EditText android:id="@+id/text_pass" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="請輸入密碼" android:inputType="textPassword" /> </LinearLayout>