Android dialog應用經驗總結

來源:互聯網
上載者:User

Android手機作業系統是一款由Google推出的開源作業系統。在智能手機領域中,這一作業系統佔據著非常重要的地位。在這裡我就先來瞭解一下Android dialog的實現方法。希望可以給大家帶來一些協助。

  • Android Theme詳細內容概述
  • Android應用技巧總結
  • Android顯示網狀圖片相關實現方法淺談
  • Android開機自啟動具體操作方法簡介
  • Android簡訊發送功能實現技巧分享

1、網上說的很多,Android dialog實現的方法有兩個

一個是通過AlertDialog.Builder 初始化dialog 然後再showDialog

另一個是通過將androidManifest.xml中的activity的屬性設為android:theme="@android:style/Theme.Dialog,偽裝為dialog

2、showDialog的線程問題

Android dialog的顯示不會阻塞ui線程.....

例子

Java代碼

 
  1. protected void onListItemClick(ListView l, View v, 
    int position, long id) {   
  2. Intent intent = new Intent();   
  3. Bundle bundle = new Bundle();   
  4. switch (editMode) {   
  5. case SELECT:   
  6. bundle.putString("listName", list.get
    (position).getName());   
  7. intent.setClass(this, AudioPlayer.class);   
  8. intent.putExtras(bundle);   
  9. startActivity(intent);   
  10. break;   
  11. case RENAME:   
  12. oldName = list.get(position).getName();   
  13. intent.setClass(MusicList.this, DialogActivity.class);   
  14. startActivityForResult(intent, Preferences.RENAME);   
  15. break;   
  16. case DELETE:   
  17. oldName = list.get(position).getName();   
  18. showDialog(CONFIRM_DIALOG);   
  19. delete(oldName,flag);   
  20. break;   
  21. }   
  22. editMode = EditMode.SELECT;   
  23. protected Dialog onCreateDialog(int id) {   
  24. switch (id) {   
  25. case CONFIRM_DIALOG:   
  26. return new AlertDialog.Builder(MusicList.this).setIcon(   
  27. android.R.drawable.ic_dialog_alert).setTitle("確認刪除?")   
  28. .setPositiveButton(R.string.confirm,   
  29. new DialogInterface.OnClickListener() {   
  30. public void onClick(DialogInterface dialog,   
  31. int whichButton) {   
  32. mListTool.deleteList(oldName);   
  33. flag = true;   
  34. }   
  35. }).setNegativeButton(R.string.cancel,   
  36. new DialogInterface.OnClickListener() {   
  37. public void onClick(DialogInterface dialog,   
  38. int whichButton) {   
  39. flag = false;   
  40. }   
  41. }).create();   
  42. }   
  43. return null;   
  44. } }   
  45. protected void onListItemClick(ListView l, View v, 
    int position, long id) {  
  46. Intent intent = new Intent();  
  47. Bundle bundle = new Bundle();  
  48. switch (editMode) {  
  49. case SELECT:  
  50. bundle.putString("listName", list.get(position).getName());  
  51. intent.setClass(this, AudioPlayer.class);  
  52. intent.putExtras(bundle);  
  53. startActivity(intent);  
  54. break;  
  55. case RENAME:  
  56. oldName = list.get(position).getName();  
  57. intent.setClass(MusicList.this, DialogActivity.class);  
  58. startActivityForResult(intent, Preferences.RENAME);  
  59. break;  
  60. case DELETE:  
  61. oldName = list.get(position).getName();  
  62. showDialog(CONFIRM_DIALOG);  
  63. delete(oldName,flag);  
  64. break;  
  65. }  
  66. editMode = EditMode.SELECT;  
  67. protected Dialog onCreateDialog(int id) {  
  68. switch (id) {  
  69. case CONFIRM_DIALOG:  
  70. return new AlertDialog.Builder(MusicList.this).setIcon(  
  71. android.R.drawable.ic_dialog_alert).setTitle("確認刪除?")  
  72. .setPositiveButton(R.string.confirm,  
  73. new DialogInterface.OnClickListener() {  
  74. public void onClick(DialogInterface dialog,  
  75. int whichButton) {  
  76. mListTool.deleteList(oldName);  
  77. flag = true;  
  78. }  
  79. }).setNegativeButton(R.string.cancel,  
  80. new DialogInterface.OnClickListener() {  
  81. public void onClick(DialogInterface dialog,  
  82. int whichButton) {  
  83. flag = false;  
  84. }  
  85. }).create();  
  86.  
  87. }  
  88. return null;  
  89. } } 

Android dialog的對話方塊還在初始化得過程中,delete方法就調用了,說明dialog是另開一個線程的,同時提供回調方法

3、取得dialog中Edittext的內容問題

由於Android dialog本身沒有提供取得Edittext內容的回呼函數,所以需要自己寫.....

簡單的方法是使用activity偽裝dialog,有布局更自由,訊息傳遞更方便地優點

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.