1、網上說的很多,Android dialog實作的方法有兩個
一個是通過AlertDialog.Builder 初始化dialog 然後再showDialog
另一個是通過将androidManifest.xml中的activity的屬性設為android:theme="@android:style/Theme.Dialog,僞裝為dialog
2、showDialog的線程問題
Android dialog的顯示不會阻塞ui線程.....
例子
-
protected void onListItemClick(ListView l, View v,
int position, long id) {
- Intent intent = new Intent();
- Bundle bundle = new Bundle();
- switch (editMode) {
- case SELECT:
-
bundle.putString("listName", list.get
(position).getName());
- intent.setClass(this, AudioPlayer.class);
- intent.putExtras(bundle);
- startActivity(intent);
- break;
- case RENAME:
- oldName = list.get(position).getName();
- intent.setClass(MusicList.this, DialogActivity.class);
- startActivityForResult(intent, Preferences.RENAME);
- break;
- case DELETE:
- oldName = list.get(position).getName();
- showDialog(CONFIRM_DIALOG);
- delete(oldName,flag);
- break;
- }
- editMode = EditMode.SELECT;
- protected Dialog onCreateDialog(int id) {
- switch (id) {
- case CONFIRM_DIALOG:
- return new AlertDialog.Builder(MusicList.this).setIcon(
- android.R.drawable.ic_dialog_alert).setTitle("确認删除?")
- .setPositiveButton(R.string.confirm,
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog,
- int whichButton) {
- mListTool.deleteList(oldName);
- flag = true;
- }
- }).setNegativeButton(R.string.cancel,
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog,
- int whichButton) {
- flag = false;
- }
- }).create();
- }
- return null;
- } }
-
protected void onListItemClick(ListView l, View v,
int position, long id) {
- Intent intent = new Intent();
- Bundle bundle = new Bundle();
- switch (editMode) {
- case SELECT:
- bundle.putString("listName", list.get(position).getName());
- intent.setClass(this, AudioPlayer.class);
- intent.putExtras(bundle);
- startActivity(intent);
- break;
- case RENAME:
- oldName = list.get(position).getName();
- intent.setClass(MusicList.this, DialogActivity.class);
- startActivityForResult(intent, Preferences.RENAME);
- break;
- case DELETE:
- oldName = list.get(position).getName();
- showDialog(CONFIRM_DIALOG);
- delete(oldName,flag);
- break;
- }
- editMode = EditMode.SELECT;
- protected Dialog onCreateDialog(int id) {
- switch (id) {
- case CONFIRM_DIALOG:
- return new AlertDialog.Builder(MusicList.this).setIcon(
- android.R.drawable.ic_dialog_alert).setTitle("确認删除?")
- .setPositiveButton(R.string.confirm,
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog,
- int whichButton) {
- mListTool.deleteList(oldName);
- flag = true;
- }
- }).setNegativeButton(R.string.cancel,
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog,
- int whichButton) {
- flag = false;
- }
- }).create();
- }
- return null;
- } }
Android dialog的對話框還在初始化得過程中,delete方法就調用了,說明dialog是另開一個線程的,同時提供回調方法
3、取得dialog中Edittext的内容問題
由于Android dialog本身沒有提供取得Edittext内容的回調函數,是以需要自己寫.....
簡單的方法是使用activity僞裝dialog,有布局更自由,消息傳遞更友善地優點