-----------------------------各式各样的对话框---------------------------------------------
首先把整体的代码贴在下面,然后是局部的讲解:
可以先把程序跑出来,对照分部的内容边演示边学习
package com.android.study; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.app.ProgressDialog; import android.content.DialogInterface; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; import android.database.Cursor; import android.provider.ContactsContract; public class MainActivity extends Activity { private static final int DIALOG_YES_NO_MESSAGE = 1; private static final int DIALOG_YES_NO_LONG_MESSAGE = 2; private static final int DIALOG_LIST = 3; private static final int DIALOG_PROGRESS = 4; private static final int DIALOG_SINGLE_CHOICE = 5; private static final int DIALOG_MULTIPLE_CHOICE = 6; private static final int DIALOG_TEXT_ENTRY = 7; private static final int DIALOG_MULTIPLE_CHOICE_CURSOR = 8; private static final int DIALOG_YES_NO_ULTRA_LONG_MESSAGE = 9; private static final int DIALOG_YES_NO_OLD_SCHOOL_MESSAGE = 10; private static final int DIALOG_YES_NO_HOLO_LIGHT_MESSAGE = 11; private static final int MAX_PROGRESS = 100; private ProgressDialog mProgressDialog; private int mProgress; private Handler mProgressHandler; @Override protected Dialog onCreateDialog(int id) { switch (id) { case DIALOG_YES_NO_MESSAGE: return new AlertDialog.Builder(MainActivity.this) .setIconAttribute(android.R.attr.alertDialogIcon) .setTitle(R.string.alert_dialog_two_buttons_title) .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { } }) .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { /* User clicked Cancel so do some stuff */ } }) .create(); case DIALOG_YES_NO_OLD_SCHOOL_MESSAGE: return new AlertDialog.Builder(MainActivity.this, AlertDialog.THEME_TRADITIONAL) .setIconAttribute(android.R.attr.alertDialogIcon) .setTitle(R.string.alert_dialog_two_buttons_title) .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { } }) .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { } }) .create(); case DIALOG_YES_NO_HOLO_LIGHT_MESSAGE: return new AlertDialog.Builder(MainActivity.this, AlertDialog.THEME_HOLO_LIGHT) .setIconAttribute(android.R.attr.alertDialogIcon) .setTitle(R.string.alert_dialog_two_buttons_title) .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { } }) .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { } }) .create(); case DIALOG_YES_NO_LONG_MESSAGE: return new AlertDialog.Builder(MainActivity.this) .setIconAttribute(android.R.attr.alertDialogIcon) .setTitle(R.string.alert_dialog_two_buttons_msg) .setMessage(R.string.alert_dialog_two_buttons2_msg) .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { /* User clicked OK so do some stuff */ } }) .setNeutralButton(R.string.alert_dialog_something, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { /* User clicked Something so do some stuff */ } }) .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { /* User clicked Cancel so do some stuff */ } }) .create(); case DIALOG_YES_NO_ULTRA_LONG_MESSAGE: return new AlertDialog.Builder(MainActivity.this) .setIconAttribute(android.R.attr.alertDialogIcon) .setTitle(R.string.alert_dialog_two_buttons_msg) .setMessage(R.string.alert_dialog_two_buttons2ultra_msg) .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { /* User clicked OK so do some stuff */ } }) .setNeutralButton(R.string.alert_dialog_something, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { /* User clicked Something so do some stuff */ } }) .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { /* User clicked Cancel so do some stuff */ } }) .create(); case DIALOG_LIST: return new AlertDialog.Builder(MainActivity.this) .setTitle(R.string.select_dialog) .setItems(R.array.select_dialog_items, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { /* User clicked so do some stuff */ String[] items = getResources().getStringArray(R.array.select_dialog_items); new AlertDialog.Builder(MainActivity.this) .setMessage("You selected: " + which + " , " + items[which]) .show(); } }) .create(); case DIALOG_PROGRESS: mProgressDialog = new ProgressDialog(MainActivity.this); mProgressDialog.setIconAttribute(android.R.attr.alertDialogIcon); mProgressDialog.setTitle(R.string.select_dialog); mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); mProgressDialog.setMax(MAX_PROGRESS); mProgressDialog.setButton(DialogInterface.BUTTON_POSITIVE, getText(R.string.alert_dialog_hide), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { /* User clicked Yes so do some stuff */ } }); mProgressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, getText(R.string.alert_dialog_cancel), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { /* User clicked No so do some stuff */ } }); return mProgressDialog; case DIALOG_SINGLE_CHOICE: return new AlertDialog.Builder(MainActivity.this) .setIconAttribute(android.R.attr.alertDialogIcon) .setTitle(R.string.alert_dialog_single_choice) .setSingleChoiceItems(R.array.select_dialog_items2, 0, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { /* User clicked on a radio button do some stuff */ } }) .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { /* User clicked Yes so do some stuff */ } }) .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { /* User clicked No so do some stuff */ } }) .create(); case DIALOG_MULTIPLE_CHOICE: return new AlertDialog.Builder(MainActivity.this) .setIcon(R.drawable.ic_launcher ) .setTitle(R.string.alert_dialog_multi_choice) .setMultiChoiceItems(R.array.select_dialog_items3, new boolean[]{false, true, false, true, false, false, false}, new DialogInterface.OnMultiChoiceClickListener() { public void onClick(DialogInterface dialog, int whichButton, boolean isChecked) { /* User clicked on a check box do some stuff */ } }) .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { /* User clicked Yes so do some stuff */ } }) .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { /* User clicked No so do some stuff */ } }) .create(); case DIALOG_MULTIPLE_CHOICE_CURSOR: String[] projection = new String[] { ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Contacts.SEND_TO_VOICEMAIL }; Cursor cursor = managedQuery(ContactsContract.Contacts.CONTENT_URI, projection, null, null, null); return new AlertDialog.Builder(MainActivity.this) .setIcon(R.drawable.ic_launcher ) .setTitle(R.string.alert_dialog_multi_choice_cursor) .setMultiChoiceItems(cursor, ContactsContract.Contacts.SEND_TO_VOICEMAIL, ContactsContract.Contacts.DISPLAY_NAME, new DialogInterface.OnMultiChoiceClickListener() { public void onClick(DialogInterface dialog, int whichButton, boolean isChecked) { Toast.makeText(MainActivity.this, "Readonly Demo Only - Data will not be updated", Toast.LENGTH_SHORT).show(); } }) .create(); case DIALOG_TEXT_ENTRY: // This example shows how to add a custom layout to an AlertDialog LayoutInflater factory = LayoutInflater.from(this); final View textEntryView = factory.inflate(R.layout.entry, null); return new AlertDialog.Builder(MainActivity.this) .setIconAttribute(android.R.attr.alertDialogIcon) .setTitle(R.string.alert_dialog_text_entry) .setView(textEntryView) .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { /* User clicked OK so do some stuff */ } }) .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { /* User clicked cancel so do some stuff */ } }) .create(); } return null; } /** * Initialization of the Activity after it is first created. Must at least * call {@link android.app.Activity#setContentView(int)} to * describe what is to be displayed in the screen. */ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); /* Display a text message with yes/no buttons and handle each message as well as the cancel action */ Button twoButtonsTitle = (Button) findViewById(R.id.two_buttons); twoButtonsTitle.setOnClickListener(new OnClickListener() { public void onClick(View v) { showDialog(DIALOG_YES_NO_MESSAGE); } }); /* Display a long text message with yes/no buttons and handle each message as well as the cancel action */ Button twoButtons2Title = (Button) findViewById(R.id.two_buttons2); twoButtons2Title.setOnClickListener(new OnClickListener() { public void onClick(View v) { showDialog(DIALOG_YES_NO_LONG_MESSAGE); } }); /* Display an ultra long text message with yes/no buttons and handle each message as well as the cancel action */ Button twoButtons2UltraTitle = (Button) findViewById(R.id.two_buttons2ultra); twoButtons2UltraTitle.setOnClickListener(new OnClickListener() { public void onClick(View v) { showDialog(DIALOG_YES_NO_ULTRA_LONG_MESSAGE); } }); /* Display a list of items */ Button selectButton = (Button) findViewById(R.id.select_button); selectButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { showDialog(DIALOG_LIST); } }); /* Display a custom progress bar */ Button progressButton = (Button) findViewById(R.id.progress_button); progressButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { showDialog(DIALOG_PROGRESS); mProgress = 0; mProgressDialog.setProgress(0); mProgressHandler.sendEmptyMessage(0); } }); /* Display a radio button group */ Button radioButton = (Button) findViewById(R.id.radio_button); radioButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { showDialog(DIALOG_SINGLE_CHOICE); } }); /* Display a list of checkboxes */ Button checkBox = (Button) findViewById(R.id.checkbox_button); checkBox.setOnClickListener(new OnClickListener() { public void onClick(View v) { showDialog(DIALOG_MULTIPLE_CHOICE); } }); /* Display a list of checkboxes, backed by a cursor */ Button checkBox2 = (Button) findViewById(R.id.checkbox_button2); checkBox2.setOnClickListener(new OnClickListener() { public void onClick(View v) { showDialog(DIALOG_MULTIPLE_CHOICE_CURSOR); } }); /* Display a text entry dialog */ Button textEntry = (Button) findViewById(R.id.text_entry_button); textEntry.setOnClickListener(new OnClickListener() { public void onClick(View v) { showDialog(DIALOG_TEXT_ENTRY); } }); /* Two points, in the traditional theme */ Button twoButtonsOldSchoolTitle = (Button) findViewById(R.id.two_buttons_old_school); twoButtonsOldSchoolTitle.setOnClickListener(new OnClickListener() { public void onClick(View v) { showDialog(DIALOG_YES_NO_OLD_SCHOOL_MESSAGE); } }); /* Two points, in the light holographic theme */ Button twoButtonsHoloLightTitle = (Button) findViewById(R.id.two_buttons_holo_light); twoButtonsHoloLightTitle.setOnClickListener(new OnClickListener() { public void onClick(View v) { showDialog(DIALOG_YES_NO_HOLO_LIGHT_MESSAGE); } }); mProgressHandler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); if (mProgress >= MAX_PROGRESS) { mProgressDialog.dismiss(); } else { mProgress++; mProgressDialog.incrementProgressBy(1); mProgressHandler.sendEmptyMessageDelayed(0, 100); } } }; } }----------------------好长的代码,看不懂呢------------------------------------------------------------------
那么我来逐一的讲解:
首先是最开始的主界面:这个界面主要是通过布局文件来进行构建:
下面就是布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/two_buttons" android:text="two buttons" android:layout_height="wrap_content" android:layout_width="fill_parent" /> <Button android:id="@+id/two_buttons2" android:text="two buttons2" android:layout_height="wrap_content" android:layout_width="fill_parent" /> <Button android:id="@+id/two_buttons2ultra" android:text="button2ultra" android:layout_height="wrap_content" android:layout_width="fill_parent" /> <Button android:id="@+id/select_button" android:text="select_button" android:layout_height="wrap_content" android:layout_width="fill_parent" /> <Button android:id="@+id/progress_button" android:text="progress_button" android:layout_height="wrap_content" android:layout_width="fill_parent" /> <Button android:id="@+id/radio_button" android:text="radio button" android:layout_height="wrap_content" android:layout_width="fill_parent" /> <Button android:id="@+id/checkbox_button" android:text="check box" android:layout_height="wrap_content" android:layout_width="fill_parent" /> <Button android:id="@+id/checkbox_button2" android:text="check box 2" android:layout_height="wrap_content" android:layout_width="fill_parent" /> <Button android:id="@+id/text_entry_button" android:text="entry button" android:layout_height="wrap_content" android:layout_width="fill_parent" /> <Button android:id="@+id/two_buttons_old_school" android:text="tow buttons old school" android:layout_height="wrap_content" android:layout_width="fill_parent" /> <Button android:id="@+id/two_buttons_holo_light" android:text="buttons holo light" android:layout_height="wrap_content" android:layout_width="fill_parent" /> </LinearLayout>
对话框主要是通过onCreateDialog和showDialog进行创建和执行的,他们区分对话框主要是通过id,所以我们要提前设定id
private static final int DIALOG_YES_NO_MESSAGE = 1; private static final int DIALOG_YES_NO_LONG_MESSAGE = 2; private static final int DIALOG_LIST = 3; private static final int DIALOG_PROGRESS = 4; private static final int DIALOG_SINGLE_CHOICE = 5; private static final int DIALOG_MULTIPLE_CHOICE = 6; private static final int DIALOG_TEXT_ENTRY = 7; private static final int DIALOG_MULTIPLE_CHOICE_CURSOR = 8; private static final int DIALOG_YES_NO_ULTRA_LONG_MESSAGE = 9; private static final int DIALOG_YES_NO_OLD_SCHOOL_MESSAGE = 10; private static final int DIALOG_YES_NO_HOLO_LIGHT_MESSAGE = 11;然后在onCreateDialog里
下面我们一种一种的来看:
-------------少量提示信息和两个按钮的对话框-------------------------------------------------
通过按钮进行转入,所以要给按钮设置监听器:
Button twoButtonsTitle = (Button) findViewById(R.id.two_buttons); twoButtonsTitle.setOnClickListener(new OnClickListener() { public void onClick(View v) { showDialog(DIALOG_YES_NO_MESSAGE); } });所有的按钮基本上都这样设置监听器,故下面不再讲解。
case DIALOG_YES_NO_MESSAGE: //AlertDialog是Dialog的一个直接子类,可以设置标题和信息,可以添加两到三个按钮 //不能够直接创建,一般通过如下格式利用builder进行创建 //positiveButton是最左边的按钮,negativeButton是最右边的按钮 return new AlertDialog.Builder(MainActivity.this) .setIconAttribute(android.R.attr.alertDialogIcon) .setTitle(R.string.alert_dialog_two_buttons_title) .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { //左键被点击触发的事件 } }) .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { //右键被点击触发的事件 } }) .create();
--------------有提示信息和三个按钮的对话框---------------------------------------------------------------
多了一个setNeutralButton ,中键的设置
case DIALOG_YES_NO_OLD_SCHOOL_MESSAGE: return new AlertDialog.Builder(MainActivity.this, AlertDialog.THEME_TRADITIONAL) .setIconAttribute(android.R.attr.alertDialogIcon) .setTitle(R.string.alert_dialog_two_buttons_title) .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { } }) .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { } }) .create();
case DIALOG_LIST: return new AlertDialog.Builder(MainActivity.this) .setTitle(R.string.select_dialog) .setItems(R.array.select_dialog_items, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { /* User clicked so do some stuff */ //要在资源中设置一个文本数组 String[] items = getResources().getStringArray(R.array.select_dialog_items); //在选择设置另一个弹出的对话框对选择做出应答 new AlertDialog.Builder(MainActivity.this) .setMessage("You selected: " + which + " , " + items[which]) .show(); } }) .create();设置文本数组可能有的人不太熟悉:
strings xml文件
<string-array name="select_dialog_items"> <item> select1 item1 </item> <item> select1 item2 </item> <item> select1 item3 </item> <item> select1 item4 </item> <item> select1 item5 </item> </string-array>which代表被点击的按钮的标号
-------------------------单选项和按钮对话框--------------------------------------------------
case DIALOG_SINGLE_CHOICE: return new AlertDialog.Builder(MainActivity.this) .setIconAttribute(android.R.attr.alertDialogIcon) .setTitle(R.string.alert_dialog_single_choice) .setSingleChoiceItems(R.array.select_dialog_items2, 0, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { /* User clicked on a radio button do some stuff */ } }) .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { /* User clicked Yes so do some stuff */ } }) .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { /* User clicked No so do some stuff */ } }) .setSingleChoice () 的第一个参数是资源文本数组的id , 第二个参数是初始的默认选项,-1代表无默认,第三个参数是监听器; -------------------------多选项和按钮对话框---------------------------------------------------------------------------- case DIALOG_MULTIPLE_CHOICE: return new AlertDialog.Builder(MainActivity.this) .setIcon(R.drawable.ic_launcher ) .setTitle(R.string.alert_dialog_multi_choice) .setMultiChoiceItems(R.array.select_dialog_items3, new boolean[]{false, true, false, true, false, false, false}, new DialogInterface.OnMultiChoiceClickListener() { public void onClick(DialogInterface dialog, int whichButton, boolean isChecked) { /* User clicked on a check box do some stuff */ } }) .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { /* User clicked Yes so do some stuff */ } }) .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { /* User clicked No so do some stuff */ } }) .create(); 不多废话,和单选项的基本一样。。 -------------------------文本出入的对话框-------------------------------------------------- <pre name="code" class="javascript">case DIALOG_TEXT_ENTRY: // This example shows how to add a custom layout to an AlertDialog LayoutInflater factory = LayoutInflater.from(this); final View textEntryView = factory.inflate(R.layout.entry, null); return new AlertDialog.Builder(MainActivity.this) .setIconAttribute(android.R.attr.alertDialogIcon) .setTitle(R.string.alert_dialog_text_entry) .setView(textEntryView) .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { /* User clicked OK so do some stuff */ } }) .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { /* User clicked cancel so do some stuff */ } }) .create();LayoutInflater是用来查找xml布局文件,且对其进行实例化的,findById是具体找某个Layout中的一个控件