【翻译】(17)对话框
see
http://developer.android.com/guide/topics/ui/dialogs.html
原文见
http://developer.android.com/guide/topics/ui/dialogs.html
-------------------------------
Dialogs
对话框
-------------------------------
In this document
本文目录
* Showing a Dialog 显示一个对话框
* Dismissing a Dialog 解除一个对话框
* Creating an AlertDialog 创建一个AlertDialog
* Adding buttons 添加按钮
* Adding a list 添加一个列表
* Creating a ProgressDialog 创建一个ProgressDialog
* Showing a progress bar 显示一个进度条
* Creating a Custom Dialog 创建一个自定义对话框
Key classes
关键类
Dialog
AlertDialog
DialogFragment
Related tutorials
相关教程
Hello DatePicker
Hello TimePicker
-------------------------------
A dialog is usually a small window that appears in front of the current Activity. The underlying Activity loses focus and the dialog accepts all user interaction. Dialogs are normally used for notifications that should interupt the user and to perform short tasks that directly relate to the application in progress (such as a progress bar or a login prompt).
一个对话框通常是一个出现在当前活动前方的小窗口。底部的活动失去焦点而该对话框接受所有用户交互。对话框通常被用于应该能打断用户的通知并且执行在进度中直接与应用程序有关的短期任务(诸如一个进度条或一个登陆提示符)。
The Dialog class is the base class for creating dialogs. However, you typically should not instantiate a Dialog directly. Instead, you should use one of the following subclasses:
Dialog类是用于创建对话框的基类。然而,你通常不应该直接实例化Dialog。取而代之的是,你应该使用以下的其中一个子类:
* AlertDialog
A dialog that can manage zero, one, two, or three buttons, and/or a list of selectable items that can include checkboxes or radio buttons. The AlertDialog is capable of constructing most dialog user interfaces and is the suggested dialog type. See Creating an AlertDialog below.
一种对话框,可以管理零个,一个,两个,或三个按钮,和/或一个可包含复选框或单选按钮的可选条目列表。AlertDialog有能力构建大多数对话框的用户界面并且是建议的对话框类型。参见下面创建一个AlertDialog章节。
* ProgressDialog
A dialog that displays a progress wheel or progress bar. Because it's an extension of the AlertDialog, it also supports buttons. See Creating a ProgressDialog below.
一种对话框,显示一个进度轮或进度条。因为它是AlertDialog的扩展,所以它还支持按钮。参见下面的创建一个ProgressDialog章节。
* DatePickerDialog
A dialog that allows the user to select a date. See the Hello DatePicker tutorial.
一种允许用户选取数据的对话框。参见你好DatePicker教程。
* TimePickerDialog
A dialog that allows the user to select a time. See the Hello TimePicker tutorial.
一种对话框,允许用户选取一个时间。参见你好TimePicker教程。
If you would like to customize your own dialog, you can extend the base Dialog object or any of the subclasses listed above and define a new layout. See the section on Creating a Custom Dialog below.
如果你喜欢定制你自己的对话框,你可以扩展Dialog基类对象或上面列举的任意一种子类以及定义一种新的布局。参见下面关于创建自定义对话框的章节。
-------------------------------
Showing a Dialog
显示一个对话框
A dialog is always created and displayed as a part of an Activity. You should normally create dialogs from within your Activity's onCreateDialog(int) callback method. When you use this callback, the Android system automatically manages the state of each dialog and hooks them to the Activity, effectively making it the "owner" of each dialog. As such, each dialog inherits certain properties from the Activity. For example, when a dialog is open, the Menu key reveals the options menu defined for the Activity and the volume keys modify the audio stream used by the Activity.
一个对话框总是被创建和显示作为活动的一部分。你通常应该从你的Activity的onCreateDialog(int)回调方法中创建对话框。当你使用这个回调时,Android系统自动地管理每个对话框的状态并把它们挂钩到Activity,有效地把它作为每个对话框的“拥有者”。因此,每个对话框从Activity中继承某些属性。例如,当一个对话框被打开时,菜单键会展开Activity定义的选项菜单而音量键修改Activity使用的音频流。
-------------------------------
Note: If you decide to create a dialog outside of the onCreateDialog() method, it will not be attached to an Activity. You can, however, attach it to an Activity with setOwnerActivity(Activity).
注意:如果你决定在onCreateDialog()方法外创建一个对话框,那么它将不会被依附到一个Activity。然而,你可以用setOwnerActivity(Activity)把它依附到一个Activity。
-------------------------------
When you want to show a dialog, call showDialog(int) and pass it an integer that uniquely identifies the dialog that you want to display.
当你希望显示一个对话框时,请调用showDialog(int)并传给它唯一标识你想显示的对话框的整数。
When a dialog is requested for the first time, Android calls onCreateDialog(int) from your Activity, which is where you should instantiate the Dialog. This callback method is passed the same ID that you passed to showDialog(int). After you create the Dialog, return the object at the end of the method.
当一个对话框首次被请求时,Android从你的Activity中调用onCreateDialog(int),在那里你应该实例化该Dialog。这个回调方法传进你传给showDialog(int)的相同ID。在你创建Dialog后,在方法的结束处返回该对象。
Before the dialog is displayed, Android also calls the optional callback method onPrepareDialog(int, Dialog). Define this method if you want to change any properties of the dialog each time it is opened. This method is called every time a dialog is opened, whereas onCreateDialog(int) is only called the very first time a dialog is opened. If you don't define onPrepareDialog(), then the dialog will remain the same as it was the previous time it was opened. This method is also passed the dialog's ID, along with the Dialog object you created in onCreateDialog().
在对话框被显示前,Android还会调用可选的回调方法onPrepareDialog(int, Dialog)。如果你希望每当对话框被打开时改变它的任意属性,请定义此方法。每次对话框被打开时此方法被调用,但是onCreateDialog(int)只会在对话框最开始被打开时被调用。如果你不定义onPrepareDialog(),那么对话框将保留与前一次被打开时相同的样子。这个方法还会传入对话框的ID,以及你在onCreateDialog()中创建的Dialog对象。
The best way to define the onCreateDialog(int) and onPrepareDialog(int, Dialog) callback methods is with a switch statement that checks the id parameter that's passed into the method. Each case should check for a unique dialog ID and then create and define the respective Dialog. For example, imagine a game that uses two different dialogs: one to indicate that the game has paused and another to indicate that the game is over. First, define an integer ID for each dialog:
定义onCreateDialog(int)和onPrepareDialog(int, Dialog)回调方法的最佳方法是用一个switch语句检查传进该方法的id参数。每个case语句应该检查一个唯一的对话框ID,然后创建并定义相应的Dialog。例如,想象一个游戏使用两个不同的对话框:一个用于指示游戏已经暂停,而另一个指示游戏结束。首先,为每个对话框定义一个整型ID。
-------------------------------
static final int DIALOG_PAUSED_ID = 0;
static final int DIALOG_GAMEOVER_ID = 1;
-------------------------------
Then, define the onCreateDialog(int) callback with a switch case for each ID:
然后,定义onCreateDialog(int)回调,用一个switch进行每个ID的case判断:
-------------------------------
protected Dialog onCreateDialog(int id) {
Dialog dialog;
switch(id) {
case DIALOG_PAUSED_ID:
// do the work to define the pause Dialog
// 执行一些工作以定义暂停对话框
break;
case DIALOG_GAMEOVER_ID:
// do the work to define the game over Dialog
// 执行一些工作以定义游戏结束对话框
break;
default:
dialog = null;
}
return dialog;
}
-------------------------------
-------------------------------
Note: In this example, there's no code inside the case statements because the procedure for defining your Dialog is outside the scope of this section. See the section below about Creating an AlertDialog, offers code suitable for this example.
注意:在此示例中,在case语句中没有代码,因为定义你的Dialog的过程在这个章节的讨论范围之外。参见下面关于创建一个AlertDialog的章节,提供适合此示例的代码。
-------------------------------
When it's time to show one of the dialogs, call showDialog(int) with the ID of a dialog:
当到了要显示其中一个对话框的时候,用某个对话框的ID调用showDialog(int):
-------------------------------
showDialog(DIALOG_PAUSED_ID);
-------------------------------
-------------------------------
Dismissing a Dialog
解除一个对话框
When you're ready to close your dialog, you can dismiss it by calling dismiss() on the Dialog object. If necessary, you can also call dismissDialog(int) from the Activity, which effectively calls dismiss() on the Dialog for you.
当你准备要关闭你的对话框时,你可以通过在Dialog对象上调用dismiss()来解除它。如果有必要,你还可以从Activity中调用dismissDialog(int),它会有效地为你在Dialog上调用dismiss()。
If you are using onCreateDialog(int) to manage the state of your dialogs (as discussed in the previous section), then every time your dialog is dismissed, the state of the Dialog object is retained by the Activity. If you decide that you will no longer need this object or it's important that the state is cleared, then you should call removeDialog(int). This will remove any internal references to the object and if the dialog is showing, it will dismiss it.
如果你正在使用onCreateDialog(int)来管理你的对话框状态(正如前一章节所讨论的那样),那么每次你的对话框被解除,Dialog对象的状态就会被Activity保持着。如果你决定你将不再需要这个对象或状态被清除是重要的事情,那么你应该调用removeDialog(int)。它将移除任意对该对象的内部引用,并且如果对话框正在显示,那么它将被解除。
Using dismiss listeners
使用解除监听器
If you'd like your application to perform some procedures the moment that a dialog is dismissed, then you should attach an on-dismiss listener to your Dialog.
如果你喜欢你的应用在对话框被解除时执行一些过程,那么你应该依附一个解除监听器到你的Dialog。
First define the DialogInterface.OnDismissListener interface. This interface has just one method, onDismiss(DialogInterface), which will be called when the dialog is dismissed. Then simply pass your OnDismissListener implementation to setOnDismissListener().
首先定义DialogInterface.OnDismissListener接口。此接口只有一个方法,onDismiss(DialogInterface),它将在对话框被解除时被调用。然后简单地传递你的OnDismissListener实现到setOnDismissListener()。
However, note that dialogs can also be "cancelled." This is a special case that indicates the dialog was explicitly cancelled by the user. This will occur if the user presses the "back" button to close the dialog, or if the dialog explicitly calls cancel() (perhaps from a "Cancel" button in the dialog). When a dialog is cancelled, the OnDismissListener will still be notified, but if you'd like to be informed that the dialog was explicitly cancelled (and not dismissed normally), then you should register an DialogInterface.OnCancelListener with setOnCancelListener().
然而,注意对话框还可以被“取消”。这是一种特殊情况,指示对话框被用户显式取消。这将发生,如果用户按下“后退”按钮以关闭对话框,或者如果对话框显式地调用cancel()(可能来自对话框中的取消按钮)。当一个对话框被取消,OnDismissListener仍将被通知,但如果你喜欢被告知对话框是被显式取消(以及不是正常被解除),那么你应该用setOnCancelListener()注册一个DialogInterface.OnCancelListener。
-------------------------------
Creating an AlertDialog
创建一个AlertDialog
An AlertDialog is an extension of the Dialog class. It is capable of constructing most dialog user interfaces and is the suggested dialog type. You should use it for dialogs that use any of the following features:
AlertDialog是Dialog类的一个扩展。它有能力构造大多数对话框用户界面并且是建议的对话框类型。你应该使用它以获得使用以下任意特征的对话框:
* A title
* 一个标题
* A text message
* 一条文本消息
* One, two, or three buttons
* 一个,两个,或三个按钮
* A list of selectable items (with optional checkboxes or radio buttons)
* 一列可选条目(带可选的复选框或单选按钮)
To create an AlertDialog, use the AlertDialog.Builder subclass. Get a Builder with AlertDialog.Builder(Context) and then use the class's public methods to define all of the AlertDialog properties. After you're done with the Builder, retrieve the AlertDialog object with create().
为了创建一个AlertDialog,要使用AlertDialog.Builder的子类。用AlertDialog.Builder(Context)获取Builder然后使用类的public方法定义所有AlertDialog属性。在你已经处理完Builder后(注:疑有误),用create()获取AlertDialog对象。
The following topics show how to define various properties of the AlertDialog using the AlertDialog.Builder class. If you use any of the following sample code inside your onCreateDialog() callback method, you can return the resulting Dialog object to display the dialog.
下面的主题展示如何使用AlertDialog.Builder类定义不同的AlertDialog属性。如果你在你的onCreateDialog()回调方法中使用下面任意的示例代码,你可以返回结果的Dialog对象以显示对话框。
Adding buttons
添加按钮
To create an AlertDialog with side-by-side buttons like the one shown in the screenshot to the right, use the set...Button() methods:
为了用像展示在右面截屏那样的并排按钮创建一个AlertDialog,请使用set...Button()方法:
(图略:
你是否确定你想退出?
是 否
)
-------------------------------
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
MyActivity.this.finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
-------------------------------
First, add a message for the dialog with setMessage(CharSequence). Then, begin method-chaining and set the dialog to be not cancelable (so the user cannot close the dialog with the back button) with setCancelable(boolean). For each button, use one of the set...Button() methods, such as setPositiveButton(), that accepts the name for the button and a DialogInterface.OnClickListener that defines the action to take when the user selects the button.
首先,用setMessage(CharSequence)添加对话框的消息文本。然后,开始方法链并用setCancelable(boolean)设置对话框为不可取消(使用户不能用后退按钮关闭对话框)。对于每个按钮,使用其中一种set...Button()方法,诸如setPositiveButton(),接受按钮名称和定义当用户选择按钮时采取动作的DialogInterface.OnClickListener对象作为参数。
-------------------------------
Note: You can only add one of each button type to the AlertDialog. That is, you cannot have more than one "positive" button. This limits the number of possible buttons to three: positive, neutral, and negative. These names are technically irrelevant to the actual functionality of your buttons, but should help you keep track of which one does what.
注意:你只可以添加各种按钮类型的其中一种到AlertDialog。就是说,你不能拥有多于一个“肯定”按钮。它限制可能的按钮个数为三个:肯定,中立,以及否定。这些名称在技术上与你的按钮的实际功能无关,但应该帮助你了解哪个按钮做什么。
-------------------------------
Adding a list
添加一个列表
To create an AlertDialog with a list of selectable items like the one shown to the right, use the setItems() method:
为了用像右面所示的那个可选条目列表创建一个AlertDialog,使用setItems()方法:
(图略:
拾取一种颜色
红
绿
蓝
)
-------------------------------
final CharSequence[] items = {"Red", "Green", "Blue"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick a color");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert = builder.create();
-------------------------------
First, add a title to the dialog with setTitle(CharSequence). Then, add a list of selectable items with setItems(), which accepts the array of items to display and a DialogInterface.OnClickListener that defines the action to take when the user selects an item.
首先,用setTitle(CharSequence)添加标题到对话框。然后,用setItems()添加一个可选条目的列表,它接受要显示条目的数组以及定义在用户选择一个条目时采取动作的DialogInterface.OnClickListener对象作为参数。
Adding checkboxes and radio buttons
添加复选框和单选按钮
To create a list of multiple-choice items (checkboxes) or single-choice items (radio buttons) inside the dialog, use the setMultiChoiceItems() and setSingleChoiceItems() methods, respectively. If you create one of these selectable lists in the onCreateDialog() callback method, Android manages the state of the list for you. As long as the Activity is active, the dialog remembers the items that were previously selected, but when the user exits the Activity, the selection is lost.
为了在对话框内创建一列多选条目(复选框)或单选条目(单选按钮),相应地使用setMultiChoiceItems()和setSingleChoiceItems()方法。如果你在onCreateDialog()回调方法内创建这些可选列表的一种,Android会为你管理列表的状态。只要Activity是激活的,对话框就会记住之前曾经被选择的条目,但当用户退出Activity,选择就会丢失。
(图略:
拾取一种颜色
红
绿
蓝
)
-------------------------------
Note: To save the selection when the user leaves or pauses the Activity, you must properly save and restore the setting throughout the activity lifecycle. To permanently save the selections, even when the Activity process is completely shutdown, you need to save the settings with one of the Data Storage techniques.
注意:为了在用户离开或暂停活动时保存选择,你必须合适地保存和恢复设置贯穿活动的生命周期。为了持久地保存选择,即便在Activity进程被完全地关闭时,你仍然需要用其中一种数据存储技术保存设置。
-------------------------------
To create an AlertDialog with a list of single-choice items like the one shown to the right, use the same code from the previous example, but replace the setItems() method with setSingleChoiceItems():
为了用像右边所示那样的单选条目列表创建一个AlertDialog,使用前一示例中的相同代码,但把setItems()替换为setSingleChoiceItems():
-------------------------------
final CharSequence[] items = {"Red", "Green", "Blue"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick a color");
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert = builder.create();
-------------------------------
The second parameter in the setSingleChoiceItems() method is an integer value for the checkedItem, which indicates the zero-based list position of the default selected item. Use "-1" to indicate that no item should be selected by default.
setSingleChoiceItems()方法中的第二参数是一个表示checkedItem的整型值,它指示默认选中条目的从零开始的列表位置。使用-1指示默认没有条目应该被选中。
-------------------------------
Creating a ProgressDialog
创建一个ProgressDialog
A ProgressDialog is an extension of the AlertDialog class that can display a progress animation in the form of a spinning wheel, for a task with progress that's undefined, or a progress bar, for a task that has a defined progression. The dialog can also provide buttons, such as one to cancel a download.
ProgressDialog是一个AlertDialog类的扩展,它可以用纺织轮(注:纺车)的形式显示一个进度动画表示未知进度的任务,或者用进度条表示一个已知进度的任务。对话框还可以提供按钮,诸如一个用来取消下载的按钮。
(图略:
正在加载。请稍候……
)
Opening a progress dialog can be as simple as calling ProgressDialog.show(). For example, the progress dialog shown to the right can be easily achieved without managing the dialog through the onCreateDialog(int) callback, as shown here:
打开一个进度对话框可以跟调用ProgressDialog.show()一样的简单。例如,显示在右边的进度对话框可以轻易地被实现,而不需要通过onCreateDialog(int)回调来被对话框管理,正如这里展示的那样:
-------------------------------
ProgressDialog dialog = ProgressDialog.show(MyActivity.this, "",
"Loading. Please wait...", true);
-------------------------------
The first parameter is the application Context, the second is a title for the dialog (left empty), the third is the message, and the last parameter is whether the progress is indeterminate (this is only relevant when creating a progress bar, which is discussed in the next section).
第一参数是应用程序的Context,第二参数是对话框的标题(留空),第三参数是消息文本,而最后参数表示进度是否是不确定的(仅当创建一个进度条时才有意义,在下一章节中讨论)。
The default style of a progress dialog is the spinning wheel. If you want to create a progress bar that shows the loading progress with granularity, some more code is required, as discussed in the next section.
进度对话框的默认样式是纺织轮。如果你希望创建一个进度条,用间隔尺寸显示加载进度,需要更多些代码,正如下一章节中讨论的那样。
Showing a progress bar
显示一个进度条
(图略:
正在加载……
61% 61/100
)
To show the progression with an animated progress bar:
为了用动画进度条显示进度:
1. Initialize the ProgressDialog with the class constructor, ProgressDialog(Context).
1. 用类构造函数ProgressDialog(Context)初始化ProgressDialog。
2. Set the progress style to "STYLE_HORIZONTAL" with setProgressStyle(int) and set any other properties, such as the message.
2. 用setProgressStyle(int)设置进度样式为STYLE_HORIZONTAL,并且设置其它任意属性,诸如消息文本。
3. When you're ready to show the dialog, call show() or return the ProgressDialog from the onCreateDialog(int) callback.
3. 当你准备要显示对话框时,请调用show()或从onCreateDialog(int)回调中返回该ProgressDialog对象。
4. You can increment the amount of progress displayed in the bar by calling either setProgress(int) with a value for the total percentage completed so far or incrementProgressBy(int) with an incremental value to add to the total percentage completed so far.
4. 你可以通过用到目前为止完成的总百分比值调用setProgress(int),或者用添加至到目前为止完成的总百分比的增量值给incrementProgressBy(int),来增加显示在条中的进度量。
For example, your setup might look like this:
例如,你的设置可能看起来像这样:
-------------------------------
ProgressDialog progressDialog;
progressDialog = new ProgressDialog(mContext);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMessage("Loading...");
progressDialog.setCancelable(false);
-------------------------------
The setup is simple. Most of the code needed to create a progress dialog is actually involved in the process that updates it. You might find that it's necessary to create a second thread in your application for this work and then report the progress back to the Activity's UI thread with a Handler object. If you're not familiar with using additional threads with a Handler, see the example Activity below that uses a second thread to increment a progress dialog managed by the Activity.
配置是简单的。需要创建一个进度对话框的大多数代码实际上是在更新它的进程中被调用。你可能发现有必要在你的应用程序中创建第二线程做这个工作,然后用Handler对象把进度报告回Activity得用户界面线程。如果你不熟悉用Handler使用附加线程,请参见下面的示例Activity,它使用第二线程来增加被Activity管理的进度对话框(注:是指增加进度值)。
Example ProgressDialog with a second thread
带有第二线程的示例ProgressDialog(注:原文这里是折叠的)
This example uses a second thread to track the progress of a process (which actually just counts up to 100). The thread sends a Message back to the main Activity through a Handler each time progress is made. The main Activity then updates the ProgressDialog.
这个示例使用第二线程跟踪进程的进度(实际上只计数至100)。通过制造每个时间进度的Handler,该线程发送一个Message回主Activity。然后,主Activity更新ProgressDialog。
-------------------------------
package com.example.progressdialog;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class NotificationTest extends Activity {
static final int PROGRESS_DIALOG = 0;
Button button;
ProgressThread progressThread;
ProgressDialog progressDialog;
/** Called when the activity is first created. */
/** 当活动首次创建时被调用。 */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Setup the button that starts the progress dialog
// 配置启动进度对话框的按钮
button = (Button) findViewById(R.id.progressDialog);
button.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
showDialog(PROGRESS_DIALOG);
}
});
}
protected Dialog onCreateDialog(int id) {
switch(id) {
case PROGRESS_DIALOG:
progressDialog = new ProgressDialog(NotificationTest.this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMessage("Loading...");
return progressDialog;
default:
return null;
}
}
@Override
protected void onPrepareDialog(int id, Dialog dialog) {
switch(id) {
case PROGRESS_DIALOG:
progressDialog.setProgress(0);
progressThread = new ProgressThread(handler);
progressThread.start();
}
// Define the Handler that receives messages from the thread and update the progress
// 定义Handler,从线程中接收消息并更新进度
final Handler handler = new Handler() {
public void handleMessage(Message msg) {
int total = msg.arg1;
progressDialog.setProgress(total);
if (total >= 100){
dismissDialog(PROGRESS_DIALOG);
progressThread.setState(ProgressThread.STATE_DONE);
}
}
};
/** Nested class that performs progress calculations (counting) */
/** 内嵌类,执行进度计算(计数)*/
private class ProgressThread extends Thread {
Handler mHandler;
final static int STATE_DONE = 0;
final static int STATE_RUNNING = 1;
int mState;
int total;
ProgressThread(Handler h) {
mHandler = h;
}
public void run() {
mState = STATE_RUNNING;
total = 0;
while (mState == STATE_RUNNING) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
Log.e("ERROR", "Thread Interrupted");
}
Message msg = mHandler.obtainMessage();
msg.arg1 = total;
mHandler.sendMessage(msg);
total++;
}
}
/* sets the current state for the thread,
* used to stop the thread */
/* 设置线程的当前状态,用于停止线程 */
public void setState(int state) {
mState = state;
}
}
}
-------------------------------
-------------------------------
Creating a Custom Dialog
创建一个自定义对话框
If you want a customized design for a dialog, you can create your own layout for the dialog window with layout and widget elements. After you've defined your layout, pass the root View object or layout resource ID to setContentView(View).
如果你想要一个对话框的定制设计。你可以为对话框窗口用layout和widget元素创建你自己的布局。在你定义完你的布局后,把根View对象或布局资源ID传给setContentView(View)。
(图略:
自定义对话框
你好,这是一个自定义对话框!
)
For example, to create the dialog shown to the right:
例如,要创建右边展示的对话框:
1. Create an XML layout saved as custom_dialog.xml:
1. 创建一个XML布局,保存为custom_dialog.xml。
-------------------------------
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_root"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
>
<ImageView android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="10dp"
/>
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#FFF"
/>
</LinearLayout>
-------------------------------
This XML defines an ImageView and a TextView inside a LinearLayout.
这个XML在LinearLayout里定义一个ImageView和一个TextView。
2. Set the above layout as the dialog's content view and define the content for the ImageView and TextView elements:
2. 设置上面的布局作为对话框的内容视图并定义ImageView和TextView元素的内容:
-------------------------------
Context mContext = getApplicationContext();
Dialog dialog = new Dialog(mContext);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Custom Dialog");
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Hello, this is a custom dialog!");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.android);
-------------------------------
After you instantiate the Dialog, set your custom layout as the dialog's content view with setContentView(int), passing it the layout resource ID. Now that the Dialog has a defined layout, you can capture View objects from the layout with findViewById(int) and modify their content.
在你实例化对话框之后,用setContentView(int)设置你的自定义布局作为对话框的内容视图,把布局资源ID传给它。既然对话框拥有一个已定义的布局,你就可以从布局中用findViewById(int)捕获View对象并修改它们的内容。
3. That's it. You can now show the dialog as described in Showing A Dialog.
3. 就这样。你现在可以显示对话框如同显示对话框章节所描述的那样。
A dialog made with the base Dialog class must have a title. If you don't call setTitle(), then the space used for the title remains empty, but still visible. If you don't want a title at all, then you should create your custom dialog using the AlertDialog class. However, because an AlertDialog is created easiest with the AlertDialog.Builder class, you do not have access to the setContentView(int) method used above. Instead, you must use setView(View). This method accepts a View object, so you need to inflate the layout's root View object from XML.
一个用Dialog基类制作的对话框必须拥有标题。如果你不调用setTitle(),那么标题用掉的空间保留空白,但仍然可见。如果你完全不想要标题,那么你应该使用AlertDialog类创建你的自定义对话框。然而,因为创建一个AlertDialog用AlertDialog.Builder类创建最容易,所以你不会拥有上面使用的setContentView(int)方法的访问权。取而代之的是,你必须使用setView(View)。这个方法接受一个View对象,所以你需要从XML中解压布局的根View对象。
To inflate the XML layout, retrieve the LayoutInflater with getLayoutInflater() (or getSystemService()), and then call inflate(int, ViewGroup), where the first parameter is the layout resource ID and the second is the ID of the root View. At this point, you can use the inflated layout to find View objects in the layout and define the content for the ImageView and TextView elements. Then instantiate the AlertDialog.Builder and set the inflated layout for the dialog with setView(View).
为了解压XML布局,请用getLayoutInflater()(或getSystemService())取出LayoutInflater,然后调用inflate(int, ViewGroup),其中第一参数为布局资源ID而第二参数是根View的ID。此时,你可以使用被解压的布局在布局中找到View对象并定义ImageView和TextView元素的内容。然后实例化AlertDialog.Builder并用setView(View)为对话框设置被解压的布局。
Here's an example, creating a custom layout in an AlertDialog:
这里有一个示例,在一个AlertDialog里创建一个自定义布局:
-------------------------------
AlertDialog.Builder builder;
AlertDialog alertDialog;
Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_dialog,
(ViewGroup) findViewById(R.id.layout_root));
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Hello, this is a custom dialog!");
ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.android);
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
alertDialog = builder.create();
-------------------------------
Using an AlertDialog for your custom layout lets you take advantage of built-in AlertDialog features like managed buttons, selectable lists, a title, an icon and so on.
对你自定义的布局使用一个AlertDialog让你可以利用内建的AlertDialog特性如托管按钮,可选列表,标题,图标等等。
For more information, refer to the reference documentation for the Dialog and AlertDialog.Builder classes.
想获取更多信息,请参阅Dialog和AlertDialog.Builder类的参考文档。
Except as noted, this content is licensed under Apache 2.0. For details and restrictions, see the Content License.
除特别说明外,本文在Apache 2.0下许可。细节和限制请参考内容许可证。
Android 4.0 r1 - 01 Dec 2011 20:53
-------------------------------
Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.
(此页部分内容基于Android开源项目,以及使用根据创作公共2.5来源许可证描述的条款进行修改)