這個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(, 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, ,
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 = ;
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();
// 每隔100毫秒發送一次消息
handler.sendEmptyMessageDelayed(, );
}
}
};
private void showProgressDialog() {
currentProgress = ;
pd = new ProgressDialog(this);
pd.setIconAttribute(android.R.attr.alertDialogIcon);
pd.setTitle("這是一個進度條對話框");
pd.setMax(MAX_PROGRESS);
pd.setProgress();
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();
}
/*
* 顯示一個帶清單的對話框,清單的條目在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();
}
}