1. 程式人生 > >Android開發筆記: 5種對話框案例

Android開發筆記: 5種對話框案例

which href 時間對話框 src orm mil end clip ack

5種android對話框
1 彈出普通對話框 --- 系統更新
2 自定義對話框-- 用戶登錄
3 時間選擇對話框 -- 時間對話框
4 進度條對話框 -- 信息加載..
5 popuWindow對話框


下載地址:http://download.csdn.net/download/taoerit/9965142

1 彈出普通對話框 --- 系統更新

[csharp] view plain copy
  1. //彈出普通對話框
  2. public void showNormalDialog(View v) {
  3. //蓋大樓的建造者:決定大樓樣子------>大樓
  4. AlertDialog.Builder builder = new Builder(this);
  5. //設置Dialog的圖標
  6. builder.setIcon(R.drawable.ic_launcher);
  7. //設置對話框的標題
  8. builder.setTitle("更新");
  9. //設置message
  10. builder.setMessage("發現新版本是否更新?");
  11. //確定按鈕 取消按鈕
  12. builder.setPositiveButton("確定",new OnClickListener() {
  13. /**
  14. * 點擊確定按鈕 回調該方法
  15. */
  16. @Override
  17. public void onClick(DialogInterface dialog, int which) {
  18. //到服務器去下載新的版本 duration單詞意思:時長
  19. Toast.makeText(MainActivity.this, "開始下載新版本", Toast.LENGTH_SHORT).show();
  20. }
  21. });
  22. builder.setNegativeButton("取消", new OnClickListener() {
  23. /**
  24. * 點擊取消按鈕 回調該方法
  25. */
  26. @Override
  27. public void onClick(DialogInterface dialog, int which) {
  28. //到服務器去下載新的版本 duration單詞意思:時長
  29. Toast.makeText(MainActivity.this, "不需要更新", Toast.LENGTH_SHORT).show();
  30. }
  31. });
  32. builder.setNeutralButton("下一次", new OnClickListener() {
  33. @Override
  34. public void onClick(DialogInterface dialog, int which) {
  35. //到服務器去下載新的版本 duration單詞意思:時長
  36. Toast.makeText(MainActivity.this, "下一次吧", Toast.LENGTH_SHORT).show();
  37. }
  38. });
  39. //通過建造這老構建一個對話框
  40. Dialog dialog = builder.create();
  41. //顯示
  42. dialog.show();
  43. }

技術分享

2 自定義對話框-- 用戶登錄

布局文件:

user_name_dialog.xml

技術分享

[csharp] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:padding="10dip"
  6. android:orientation="vertical" >
  7. <TextView
  8. android:id="@+id/tv_title"
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:text="登錄信息"
  12. android:gravity="center"
  13. android:textAppearance="?android:attr/textAppearanceLarge" />
  14. <TextView
  15. android:id="@+id/tv_name"
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:text="用戶名:" />
  19. <EditText android:id="@+id/et_name"
  20. android:layout_width="match_parent"
  21. android:layout_height="wrap_content"
  22. android:hint="請輸入用戶名"/>
  23. <TextView
  24. android:id="@+id/tv_pwd"
  25. android:layout_width="wrap_content"
  26. android:layout_height="wrap_content"
  27. android:text="密 碼:" />
  28. <EditText android:id="@+id/et_pwd"
  29. android:layout_width="match_parent"
  30. android:layout_height="wrap_content"
  31. android:inputType="textPassword"
  32. android:hint="請輸入密碼"/>
  33. <requestFocus />
  34. <Button
  35. android:id="@+id/btn_confirm"
  36. android:layout_width="150dip"
  37. android:layout_height="wrap_content"
  38. android:layout_gravity="center"
  39. android:text="登錄" />
  40. <Button
  41. android:id="@+id/btn_cancel"
  42. android:layout_width="150dip"
  43. android:layout_height="wrap_content"
  44. android:layout_gravity="center"
  45. android:text="取消" />
  46. </LinearLayout>

java代碼:

[csharp] view plain copy
  1. //自定義對話框
  2. Dialog cus_dialog ;
  3. public void showCustomDialog(View v){
  4. AlertDialog.Builder builder = new Builder(this);
  5. // 布局填充器
  6. LayoutInflater inflater = LayoutInflater.from(this);
  7. View view = inflater.inflate(R.layout.user_name_dialog, null);
  8. // 設置自定義的對話框界面
  9. builder.setView(view);
  10. // 獲取用戶名密碼
  11. final EditText name = (EditText) view.findViewById(R.id.et_name);
  12. final EditText pwd = (EditText) view.findViewById(R.id.et_pwd);
  13. Button btn_confirm = (Button) view.findViewById(R.id.btn_confirm);
  14. btn_confirm.setOnClickListener(new View.OnClickListener() {
  15. @Override
  16. public void onClick(View v) {
  17. // TODO 自動生成的方法存根
  18. if(name.getText().toString().trim().equals("abc")){
  19. showToastMsg("用戶名正確");
  20. // 對話框消失
  21. cus_dialog.dismiss();
  22. }
  23. else{
  24. showToastMsg("用戶名錯誤");
  25. }
  26. }
  27. });
  28. Button btnCancel = (Button) view.findViewById(R.id.btn_cancel);
  29. btnCancel.setOnClickListener(new View.OnClickListener() {
  30. @Override
  31. public void onClick(View v) {
  32. // 對話框消失
  33. cus_dialog.dismiss();
  34. }
  35. });
  36. cus_dialog = builder.create();
  37. cus_dialog.show();
  38. }

技術分享

3 時間選擇對話框 -- 時間對話框

[csharp] view plain copy
  1. // 時間選擇對話框
  2. public void showTimePickerDialog(View v){
  3. Calendar sysDate = Calendar.getInstance();
  4. //設置系統時間
  5. sysDate.setTimeInMillis(System.currentTimeMillis());
  6. int hour = sysDate.get(Calendar.HOUR_OF_DAY);
  7. int minute = sysDate.get(Calendar.MINUTE);
  8. TimePickerDialog time = new TimePickerDialog(this,
  9. new OnTimeSetListener() {
  10. @Override
  11. public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
  12. // TODO 自動生成的方法存根
  13. showToastMsg(" hourOfDay:" + hourOfDay + " minute:" + minute);
  14. }
  15. }, //callBack 選擇時間後的回調方法
  16. hour,//hourOfDay 當前系統時間
  17. minute,//hourOfDay 當前系統時間
  18. true);//是否24小時制
  19. time.show();
  20. }


技術分享

4 進度條對話框 -- 信息加載..

[csharp] view plain copy
  1. /**
  2. * 進度條對話框
  3. * @param v
  4. */
  5. public void showProgressDialog(View v){
  6. final ProgressDialog progress = new ProgressDialog(this);
  7. progress.setProgress(R.drawable.img2);
  8. progress.setTitle("標題");
  9. progress.setMessage("加載中...");
  10. //樣式1 進度條樣式
  11. progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
  12. //樣式2 有加載圖標
  13. //progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
  14. //最大
  15. progress.setMax(100);
  16. //當前
  17. progress.setProgress(50);
  18. progress.setButton("確定", new OnClickListener() {
  19. @Override
  20. public void onClick(DialogInterface dialog, int which) {
  21. // TODO 自動生成的方法存根
  22. showToastMsg("確定按鈕");
  23. }
  24. });
  25. progress.setButton2("取消", new OnClickListener() {
  26. @Override
  27. public void onClick(DialogInterface dialog, int which) {
  28. // TODO 自動生成的方法存根
  29. showToastMsg("取消按鈕");
  30. }
  31. });
  32. progress.show();
  33. new Thread(new Runnable() {
  34. @Override
  35. public void run() {
  36. // TODO Auto-generated method stub
  37. int i = 0;
  38. while (i < 100) {
  39. try {
  40. Thread.sleep(200);
  41. // 更新進度條的進度,可以在子線程中更新進度條進度
  42. progress.incrementProgressBy(5);
  43. // progress.incrementSecondaryProgressBy(10);//二級進度條更新方式
  44. i += 5;
  45. } catch (Exception e) {
  46. // TODO: handle exception
  47. }
  48. }
  49. // 在進度條走完時刪除Dialog
  50. progress.dismiss();
  51. }
  52. }).start();
  53. }


技術分享

5 popuWindow對話框

[csharp] view plain copy
  1. Button btn_popu;
  2. //popuWindow對話框
  3. public void showPopuWindow(View v){
  4. btn_popu = (Button) v;
  5. // 設置布局
  6. View view = LayoutInflater.from(this).inflate(R.layout.pop_window, null);
  7. PopupWindow window = new PopupWindow(this);
  8. window.setContentView(view);
  9. window.setWidth(360);
  10. window.setHeight(200);
  11. int[] location = new int[2];
  12. // 獲取按鈕坐標
  13. btn_popu.getLocationInWindow(location);
  14. window.setFocusable(true);
  15. window.setBackgroundDrawable(getResources().getDrawable(R.drawable.back_null));
  16. window.showAtLocation(btn_popu, Gravity.LEFT |Gravity.TOP , location[0]+ btn_popu.getWidth(), location[1] + 0 );
  17. //showToastMsg("" + (location[0]+ btn_popu.getWidth())+" "+ (location[1] + btn_popu.getHeight() / 2));
  18. ImageView img_start = (ImageView) view.findViewById(R.id.img_start);
  19. img_start.setOnClickListener(new View.OnClickListener() {
  20. @Override
  21. public void onClick(View v) {
  22. // TODO 自動生成的方法存根
  23. showToastMsg("點擊了啟動");
  24. }
  25. });
  26. }


技術分享

Android開發筆記: 5種對話框案例