当前位置:  编程技术>移动开发
本页文章导读:
    ▪【通译】(17)对话框        【翻译】(17)对话框 【翻译】(17)对话框   see http://developer.android.com/guide/topics/ui/dialogs.html   原文见 http://developer.android.com/guide/topics/ui/dialogs.html   -------------------------------   Dialogs   对话框  .........
    ▪ Detecting and responding to dynamic collisions        Detecting and responding to dynamic collisions. In the last post I discussed how Replica Island uses line segments organized as a 2D regular grid of tiles as the basis for its background collision system. This time I will explain how dynamic collision.........
    ▪ 小初学者赚钱记,一个入门两个月开发者的赚钱经历       小菜鸟赚钱记,一个入门两个月开发者的赚钱经历 各位师兄师弟师姐师妹,大家好!俺就先说说自己吧,本人今年7月份本科刚毕业,本科学的是电子信息工程,毕业后进了一个港企的研.........

[1]【通译】(17)对话框
    来源: 互联网  发布时间: 2014-02-18
【翻译】(17)对话框

【翻译】(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来源许可证描述的条款进行修改)

 


    
[2] Detecting and responding to dynamic collisions
    来源: 互联网  发布时间: 2014-02-18
Detecting and responding to dynamic collisions.

In the last post I discussed how Replica Island uses line segments organized as a 2D regular grid of tiles as the basis for its background collision system. This time I will explain how dynamic collisions (collisions between moving objects) are detected and resolved.

In Replica Island I draw a distinction between collisions that occur with the background geometry (falling on the ground, sliding on a slope, hitting the Android's head on the ceiling) and collisions that occur between game objects (the Android depressing a button, or hitting an enemy, or collecting a coin). While both of those cases are forms of "collision detection," they represent very different types of tests and I have two (entirely separate--mostly) systems for dealing with them.

Moving objects vs the background Since I alluded to how background collision detection works in the last post, I'll start with that system. If Android is falling through space and passes into the ground, I need to detect that intersection and then fix it so that he doesn't actually fall through the floor. This is actually a pretty tricky problem because the frame rate of any Android game (and really, any modern game on any platform) can fluctuate as the game is played. The Android moves through space in game units / second, but the game is displayed in frames per second, and depending on the current speed of the game there's no good way to predict how far he'll move in a single frame. So I need a method that can cover a range of space between the last frame and this one so that even a dramatic movement won't allow the player to pass through walls.

The solution is to "sweep" the space in between the character's position at the last frame and his current position, and snap the character back if an intersection is detected. I use rays to do this: rays are cast from a character's previous position to his current position, and the first intersection along the ray (that is, the intersection that is closest to the ray's start point) is considered to be the spot at which the character hit a wall. I also filter my ray test by the normals of the surfaces I am considering; surfaces that do not oppose the direction of the ray can be ignored (that is, I only care about surfaces whose dot product against the direction of the ray is less that 0). Characters are not well described by a single, thin ray, however, so I do two tests: one filtered against horizontal surfaces and one filtered against vertical surfaces (angled surfaces fall into one bucket or the other depending on their slope). This is a nice method because it allows me to tune exactly how I test a volume against collision; often I want to allow a small amount of intersection: when the character is standing on a sloped surface, for example, I want to allow his bounding box to intersect with the slope slightly so that it looks like his feet are on the ground. With a couple of simple ray tests this method covers the space between the previous frame and the current frame pretty well without too much processor overhead. I briefly experimented with a final volume test pass to make sure that collisions were not being missed by the ray tests, but in the end such a test wasn't necessary (and actually, despite being more technically correct, the results were a lot less fun).

Sometimes I want game objects that act like background collision but are not part of the collision tile map. For example, a moving platform might act in every way like a background element except that it can move. In these cases, I allow entities to submit temporary line segments to the background collision system which will then be used along with all the rest of the background collision line segment data. This way characters in the world can be made to act exactly like solid objects without remaining static, and other characters can come along and react to them without any special code.

Moving objects vs each other However, the more common case is when two game objects--a bullet and the player, an enemy and some spikes, etc--that are moving and non-solid come into contact. In this case we need to detect the intersection and then let code specific to each entity decide what to do about it. Still, we can generalize the system a little bit more: usually in such collisions we can name one of the entities as the "offender" and the other entity as the "victim." When a bullet hits the player, the bullet is the offender and the player is the victim. When a robot runs into some spikes, the spikes are the offender and the robot the victim. In fact, if we consider an animating character, we might want to mark some parts of a given frame of animation as "offensive" and other parts "vulnerable." In a game where a character can punch, we probably want the character's fist to deal damage to other characters but at the same time we'd expect other parts of the character, say his back and head, to be vulnerable to hits. So in order to detect collisions between game entities, I decided to give my entities multiple collision volumes, some associated with offensive areas and others associated with areas that are vulnerable to hits.

Each animation frame in Replica Island can carry a list of "attack" volumes and "vulnerability" volumes. When detecting collisions, I stipulate that collisions can only occur between a single attack volume and a single vulnerability volume. Furthermore, volumes can be set to deal and receive specific types of hits, which allows me to filter the number of actual volume intersection tests I need to perform (for example, the coin is only vulnerable to a "collection" hit, so only collection-hit-dealing attack volumes will be tested against the coin.

Each time an animation frame changes a new set of attack and vulnerability volumes may become active. These volumes are unioned together into a sphere that is guaranteed to encompass them called the bounding sphere. The volumes, along with the bounding sphere, are then submitted to the runtime collision detection system. Each frame, the collision detection system sorts all of the bounding spheres that have been submitted and tests them for intersections. The sort is by the left-most point of the sphere, so objects end up sorted along the x-axis of the level. This is a type of sweep and prune algorithm, and it makes it easy to quickly find overlapping bounding spheres because potentially colliding sphere pairs are guaranteed to be grouped together in the sorted list. When a pair of bounding spheres that intersect is found, each of the related entities' attack and vulnerability volumes are tested for intersection. If an intersection between an attack volume and a vulnerability volume is found, we know that these two entities have hit each other and some other code needs to run in order to respond.

For a long time the Replica Island engine only supported sphere collision volumes for these kinds of dynamic tests. About half-way through development I added an axis-aligned box collision type as well, but otherwise no complicated collision volume tests have been necessary. I'm very happy with the way that this system turned out: it's reliable, fast, and easy to extend.

    
[3] 小初学者赚钱记,一个入门两个月开发者的赚钱经历
    来源: 互联网  发布时间: 2014-02-18
小菜鸟赚钱记,一个入门两个月开发者的赚钱经历
各位师兄师弟师姐师妹,大家好!俺就先说说自己吧,本人今年7月份本科刚毕业,本科学的是电子信息工程,毕业后进了一个港企的研发部做嵌入式软件开发,笔试啦面试啦什么的都是跟C有关的,基本与JAVA不挂钩,更别提ANDROID开发了,实实在在的一个小菜鸟。进公司一个月,基本处于打酱油状态,不是看文档,就是些什么杂七杂八的培训,啥米企业文化啦,规章制度啦等等。。。好吧,本科生刚出来貌似都得这么开始混的吧。
    说起android开发,还真是凑巧,公司刚好接了个项目,项目具体是啥就不说了,就是得用到android架构一个服务器吧。我们公司本来就没有做这块的,这下好了两个研发部都木有人能做,于是老总就让我们这些刚进来的小年轻么边学边干吧。接下来就是自己的学习和开发经历了。。。
    记得,刚开始还特意买了安卓开发的教材,公司有JAVA的就不用自己买了,基本都打了酱油了,只是在开始时翻了翻,个人还是喜欢谷歌百度,什么环境搭建啦,第一个无脑APP也都是从谷歌百度上整的。还好上学那会儿C基础很扎实(不然也进不了这公司),所以基本就把JAVA的基础知识简单的熟络一下下。感觉差不多够用了,实在不行到时候在GOOGLE吧。我的安卓开发总算是迈出了第一步了。
    学了一个月,公司的项目也完成了一些些,安卓开发也掌握了一些些吧,反正是菜鸟。这段时间也不断的从网上了解了一些相关的信息,说什么android APP挺赚钱的,有人一天可以赚好几千甚至上万的,俺是那个激动啊,那个诱惑啊!于是想着自己能不能做出个应用赚点钱,零花钱也行。
     俺的第一个真正的应用是科学计算器,各位别拍砖,知道很多人都写过,俺这个是在完全没有参考下一步步写出来的,也算是不容易啦,功能齐全,什么三角函数,角度弧度切换,对数指数运算,阶乘等等差不多都有了。中间也遇到很多问题,印象最深刻的是浮点数处理,悲催啊,0.3乘以3得到的是0.89999999999,百度谷歌也没找到什么解决办法,后来想啊想啊,就采用计数法,后面要是有一串9或者一串0什么的,都采用相应的处理方法,反正被我弄得挺纠结的。。。最后,软件是出来了可是这个真不能赚钱,广告?不行,没地方放了,后来从广告商那边知道还有积分墙这个东东,就无脑的在我的计算器里添加了个按键,叫什么热门应用下载。大功告成了,就往网上发,各种市场的传,有通过的有不通过的,反正下载量到现在也就上千吧,赚了十来块钱,这些钱大部分是我用同学的手机下载热门应用的,还有的就是让以前的同学下载试试。。。这就相当于试水了!
    接下来,就开始打算真正的做出些东西来了,想着想着想到了我们老家这边小时候经常玩的一个纸牌速算游戏,就是算二十四点啦,不懂的自己可以查查。光是纸牌估计没啥意思吧,我就加了美女,叫什么美女速算二十四点,可能这就是所谓的市场吧,还是美女有市场。。。这个游戏前前后后花了一周多时间吧,界面什么的都设计的还好,代码大概敲了两千多行吧。音乐啦,数据库啦(用来保存游戏纪录的)等等功能也都能实现了。这回,游戏倒是添加了广告条,但是我比较无耻的采用积分限定游戏,你不下应用就不许你玩游戏。后来发现这个真不行,没准玩家在背后怎么诅咒我。。。后来就升级了,去掉积分限制,就靠广告收入,反正一个月了赚个七八十块钱吧。。。
    真正赚钱的是俺的第三款游戏,其实这款游戏说实在比第二款要简单的很多,花了不到一周时间就写好了,这回加入了广告条和积分墙,积分墙不是用来限制游戏了,而是用来查看答案的。刚开始,为了推广,就发到各个论坛,然后用流量精灵刷访问量,同时又多注册了几个论坛帐号用来顶帖用的,虽然无耻,但是为了推广没办法啊。。。那些市场的论坛只有版主才可以设定回复可见,没辙,小菜鸟只能用这种方法提高知名度了。紧跟着市场上架了,我就把论坛里的附件删除掉,但是加了链接,链接到市场那边,这样可以提高市场那边的下载量。
    后来又从网上了解了一些应用互推,相信各位也都知道,果断的升级游戏,加入了互推的SDK,这样每天都能多引进一千多的下载量,从11月16号上架到今天差不多二十天吧,下载量差不多有三万了,也不是很多,通过广告和积分墙总共赚了快两千块钱,每天赚的波动也挺大的,好的时候一天接近两百块钱,差的时候一天才四十几块钱。虽然也不是很多,但是平均下来一天八十左右的外快,作为一个刚出来的小本科生感觉还是挺幸福的,至少一个月的房租、水电、宽带、生活费什么都解决了,工资就可以存起来了,哈哈!
    总结,这两三个月,进步还是值得肯定的,从一无所知的超级菜鸟,到现在已经可以自己写游戏,写软件赚小钱,倒是可以乐呵乐呵一下。上周又把一款杂志推出来了,在各大市场也已上架了,自己还要不断努力进步,当然希望大家可以共勉一下下!

    
最新技术文章:
▪Android开发之登录验证实例教程
▪Android开发之注册登录方法示例
▪Android获取手机SIM卡运营商信息的方法
▪Android实现将已发送的短信写入短信数据库的...
▪Android发送短信功能代码
▪Android根据电话号码获得联系人头像实例代码
▪Android中GPS定位的用法实例
▪Android实现退出时关闭所有Activity的方法
▪Android实现文件的分割和组装
建站其它 iis7站长之家
▪Android双击返回键退出程序的实现方法
▪Android实现侦听电池状态显示、电量及充电动...
▪Android获取当前已连接的wifi信号强度的方法
▪Android实现动态显示或隐藏密码输入框的内容
▪根据USER-AGENT判断手机类型并跳转到相应的app...
▪Android Touch事件分发过程详解
▪Android中实现为TextView添加多个可点击的文本
▪Android程序设计之AIDL实例详解
▪Android显式启动与隐式启动Activity的区别介绍
▪Android按钮单击事件的四种常用写法总结
▪Android消息处理机制Looper和Handler详解
▪Android实现Back功能代码片段总结
▪Android实用的代码片段 常用代码总结
▪Android实现弹出键盘的方法
▪Android中通过view方式获取当前Activity的屏幕截...
▪Android提高之自定义Menu(TabMenu)实现方法
▪Android提高之多方向抽屉实现方法
▪Android提高之MediaPlayer播放网络音频的实现方法...
▪Android提高之MediaPlayer播放网络视频的实现方法...
▪Android提高之手游转电视游戏的模拟操控
 


站内导航:


特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

©2012-2021,,E-mail:www_#163.com(请将#改为@)

浙ICP备11055608号-3