Android手机操作系统是一款由谷歌推出的开源操作系统。在智能手机领域中,这一操作系统占据着非常重要的地位。在这里我就先来了解一下Android dialog的实现方法。希望可以给大家带来一些帮助。#t#
1、网上说的很多,Android dialog实现的方法有两个
一个是通过AlertDialog.Builder 初始化dialog 然后再showDialog
另一个是通过将androidManifest.xml中的activity的属性设为android:theme="@android:style/Theme.Dialog,伪装为dialog
2、showDialog的线程问题
Android dialog的显示不会阻塞ui线程.....
例子
Java代码
- 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,有布局更自由,消息传递更方便地优点