这个demo演示了各种dialogs的定义和样式。
activity_main.xml
<ScrollView 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"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp"
android:gravity="center_horizontal">
<Button
android:id="@+id/ok_cancel_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="OK Cancel dialog with a message"/>
<Button
android:id="@+id/ok_cancel_long_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="OK Cancel dialog with a long message"/>
<Button
android:id="@+id/ok_cancel_ultra_long_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="OK Cancel dialog with a ultra long message"/>
<Button
android:id="@+id/list_dialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="List Dialog"/>
<Button
android:id="@+id/progress_dialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Progress Dialog"/>
<Button
android:id="@+id/single_choice_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Single Choice List"/>
<Button
android:id="@+id/repeat_alarm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Repeat Alarm"/>
<Button
android:id="@+id/send_call_voicemail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send Call to VoiceMail"/>
<Button
android:id="@+id/send_call_voicemail_with_cursorloader"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send Call to VoiceMail With CursorLoader"/>
<Button
android:id="@+id/text_entry"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Text Entry Dialog"/>
<Button
android:id="@+id/ok_cancel_traditional_theme"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="OK Cancel dialog with traditional theme"/>
<Button
android:id="@+id/ok_cancel_holo_light"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="OK Cancel dialog with Holo Light theme"/>
</LinearLayout>
</ScrollView>
arrays.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="list_item">
<item >Command One</item>
<item >Command Two</item>
<item >Command Three</item>
<item >Command Four</item>
</string-array>
</resources>
custom_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Name"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Password"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"/>
</LinearLayout>
MainActivity
public class MainActivity extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button ok_cancel_message = (Button) findViewById(R.id.ok_cancel_message);
Button ok_cancel_long_message = (Button) findViewById(R.id.ok_cancel_long_message);
Button ok_cancel_ultra_long_message = (Button) findViewById(R.id.ok_cancel_ultra_long_message);
Button list_dialog = (Button) findViewById(R.id.list_dialog);
Button progress_dialog = (Button) findViewById(R.id.progress_dialog);
Button single_choice_list = (Button) findViewById(R.id.single_choice_list);
Button repeat_alarm = (Button) findViewById(R.id.repeat_alarm);
Button send_call_voicemail = (Button) findViewById(R.id.send_call_voicemail);
Button send_call_voicemail_with_cursorloader = (Button) findViewById(R.id.send_call_voicemail_with_cursorloader);
Button text_entry = (Button) findViewById(R.id.text_entry);
Button ok_cancel_traditional_theme = (Button) findViewById(R.id.ok_cancel_traditional_theme);
Button ok_cancel_holo_light = (Button) findViewById(R.id.ok_cancel_holo_light);
ok_cancel_message.setOnClickListener(this);
ok_cancel_long_message.setOnClickListener(this);
ok_cancel_ultra_long_message.setOnClickListener(this);
list_dialog.setOnClickListener(this);
progress_dialog.setOnClickListener(this);
single_choice_list.setOnClickListener(this);
repeat_alarm.setOnClickListener(this);
send_call_voicemail.setOnClickListener(this);
send_call_voicemail_with_cursorloader.setOnClickListener(this);
text_entry.setOnClickListener(this);
ok_cancel_traditional_theme.setOnClickListener(this);
ok_cancel_holo_light.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.ok_cancel_message:
showOkCancelMessageDialog();
break;
case R.id.ok_cancel_long_message:
showOkCancelLongMessageDialog();
break;
case R.id.ok_cancel_ultra_long_message:
showUltraLongMessageDialog();
break;
case R.id.list_dialog:
showListDialog();
break;
case R.id.progress_dialog:
showProgressDialog();
break;
case R.id.single_choice_list:
showSingleChoiceDialog();
break;
case R.id.repeat_alarm:
showMultiChoiceDialog();
break;
case R.id.send_call_voicemail:
showCursorMultiChoiceDialog();
break;
case R.id.send_call_voicemail_with_cursorloader:
showCursorMultiChoiceDialogWithCursorLoader();
break;
case R.id.text_entry:
showCustomViewDialog();
break;
case R.id.ok_cancel_traditional_theme:
showTraditionalDialog();
break;
case R.id.ok_cancel_holo_light:
showHoloLightDialog();
break;
default:
break;
}
}
/*
* 显示一个使用自定义View的对话框
*/
private void showCustomViewDialog() {
LayoutInflater inflater=LayoutInflater.from(this);
View view=inflater.inflate(R.layout.custom_view, null);
AlertDialog.Builder builder = new Builder(MainActivity.this);
builder.setIconAttribute(android.R.attr.alertDialogIcon);
builder.setTitle("这是一个使用了自定义View的对话框");
builder.setView(view);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.show();
}
/*
* 使用cursorloader完成查询结果集作为列表项的多选对话框
*/
private String[] projections = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.SEND_TO_VOICEMAIL };
private LoaderCallbacks<Cursor> myLoader = new LoaderCallbacks<Cursor>() {
@Override
public void onLoaderReset(Loader<Cursor> loader) {
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
AlertDialog.Builder builder = new Builder(MainActivity.this);
builder.setIconAttribute(android.R.attr.alertDialogIcon);
builder.setTitle("这是一个使用CursorLoader方法得到查询结果集,并将其作为列表项的多选对话框!");
builder.setMultiChoiceItems(cursor,
ContactsContract.Contacts.SEND_TO_VOICEMAIL,
ContactsContract.Contacts.DISPLAY_NAME,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
// TODO Auto-generated method stub
}
});
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.show();
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
return new CursorLoader(MainActivity.this,
ContactsContract.Contacts.CONTENT_URI, projections, null,
null, null);
}
};
private void showCursorMultiChoiceDialogWithCursorLoader() {
getLoaderManager().initLoader(0, null, myLoader);
}
/*
* 将一个查询结果集做为多选对话框的列表项,这里从通讯录中查询出联系人的名字,作为列表项。
* 这个方法中使用了一个过时的查询方法,如果数据量较小的话,这种查询方法影响不大,可以忽略
* 过时的说明,继续使用。如果有看到这条删除线就不舒服的强迫症患者的话,下个方法中我们使用 了CursorLoader的方法.
* 需要注意的是别忘了添加读取通讯录的权限
*/
private void showCursorMultiChoiceDialog() {
String[] projection = new String[] { ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.SEND_TO_VOICEMAIL };
@SuppressWarnings("deprecation")
Cursor cursor = managedQuery(ContactsContract.Contacts.CONTENT_URI,
projection, null, null, null);
AlertDialog.Builder builder = new Builder(this);
builder.setIconAttribute(android.R.attr.alertDialogIcon);
builder.setTitle("这是一个将查询结果集作为列表项的多选对话框!");
builder.setMultiChoiceItems(cursor,
ContactsContract.Contacts.SEND_TO_VOICEMAIL,
ContactsContract.Contacts.DISPLAY_NAME,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
// TODO Auto-generated method stub
}
});
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.show();
}
/*
* 显示一个多选列表对话框
*/
private void showMultiChoiceDialog() {
AlertDialog.Builder builder = new Builder(this);
builder.setIconAttribute(android.R.attr.alertDialogIcon);
builder.setTitle("这是一个多选列表对话框!");
boolean[] checkedItems = new boolean[] { false, true, false, false };
builder.setMultiChoiceItems(R.array.list_item, checkedItems,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
// TODO Auto-generated method stub
}
});
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.show();
}
/*
* 显示一个单选列表对话框
*/
private void showSingleChoiceDialog() {
AlertDialog.Builder builder = new Builder(this);
builder.setIconAttribute(android.R.attr.alertDialogIcon);
builder.setTitle("这是一个单选列表对话框!");
builder.setSingleChoiceItems(R.array.list_item, 0,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.show();
}
/*
* 显示一个进度条对话框,这里需要注意的是要得到进度条对话框需要创建一个进度条对话的实例 并需要设置进度条的最大值和进度条的样式
*/
private ProgressDialog pd;
private static final int MAX_PROGRESS = 100;
private int currentProgress;
// 用于处理进度条的进度更新效果
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (currentProgress >= MAX_PROGRESS) {
pd.dismiss();
} else {
currentProgress++;
pd.incrementProgressBy(1);
// 每隔100毫秒发送一次消息
handler.sendEmptyMessageDelayed(0, 100);
}
}
};
private void showProgressDialog() {
currentProgress = 0;
pd = new ProgressDialog(this);
pd.setIconAttribute(android.R.attr.alertDialogIcon);
pd.setTitle("这是一个进度条对话框");
pd.setMax(MAX_PROGRESS);
pd.setProgress(0);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setButton(AlertDialog.BUTTON_POSITIVE, "Hide",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
pd.setButton(AlertDialog.BUTTON_NEGATIVE, "Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
pd.show();
handler.sendEmptyMessage(0);
}
/*
* 显示一个带列表的对话框,列表的条目在res/values目录下的arrays.xml中定义
*/
private void showListDialog() {
AlertDialog.Builder builder = new Builder(this);
builder.setIconAttribute(android.R.attr.alertDialogIcon);
builder.setTitle("这是一个带有列表的对话框!");
builder.setItems(R.array.list_item,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String[] items = getResources().getStringArray(
R.array.list_item);
CharSequence title = "You selected:" + which + ","
+ items[which];
new AlertDialog.Builder(MainActivity.this).setTitle(
title).show();
}
});
builder.show();
}
/*
* 显示一个携带超长信息的对话框
*/
private void showUltraLongMessageDialog() {
AlertDialog.Builder builder = new Builder(this);
builder.setIconAttribute(android.R.attr.alertDialogIcon);
builder.setTitle("这是一个带有超长信息的对话框!");
builder.setMessage(R.string.ultra_long_message);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.show();
}
/*
* 显示一个HoloLight样式的对话框
*/
private void showHoloLightDialog() {
AlertDialog.Builder builder = new Builder(this,
AlertDialog.THEME_HOLO_LIGHT);
builder.setIconAttribute(android.R.attr.alertDialogIcon);
builder.setTitle("这是一个HoloLight样式的对话框!");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.show();
}
/*
* 显示一个传统样式的对话框
*/
private void showTraditionalDialog() {
AlertDialog.Builder builder = new Builder(this,
AlertDialog.THEME_TRADITIONAL);
builder.setIconAttribute(android.R.attr.alertDialogIcon);
builder.setTitle("这是一个传统样式的对话框!");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.show();
}
/*
* 显示一个携带长消息的对话框
*/
private void showOkCancelLongMessageDialog() {
AlertDialog.Builder builder = new Builder(this);
builder.setIconAttribute(android.R.attr.alertDialogIcon);
builder.setTitle("这是一个带有长信息的对话框!");
builder.setMessage(R.string.long_text);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.show();
}
/*
* 显示一个不带信息的对话框,只将标题作为简单的提示信息
*/
private void showOkCancelMessageDialog() {
// 获取对话框的构建器
AlertDialog.Builder builder = new Builder(this);
// 设置图标
builder.setIconAttribute(android.R.attr.alertDialogIcon);
// 设置标题
builder.setTitle("这是一个将标题作为简单提示信息的对话框!");
// 设置确定按钮
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
// 设置取消按钮
builder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
// 显示对话框
builder.show();
}
}