Android 自定义 dialog

来源:互联网 发布:苏州园林 知乎 编辑:程序博客网 时间:2024/04/29 21:54

Andoid Dialog

1、AlertDialog,具有0-3个按钮,可以放选项、复选框单选框等,以建议的方式域用户交互可以。
2、ProgressDialog,显示一个进度的圆环或者进度条。
3、DataPickerDialog,选择日期的dialog。
4、TimePickerDialog,选择时间的dialog。

用户可以继承Dialog类或者它的子类并且创建一个新的leyout。

Showing a Dialog

Dialog总是作为一个Activity的一部分来创建和显示的。正常可以使用Activity的onCreateDialog(int)回调函数来创建Dialog。使用这个回调函数的时候,系统会自动管理每个dialog的状态,并把它们关联到这个Activity,有效的使它成为dialog的所有者。这样每个第啊咯个都从父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.
注意:如果你在onCreate()之外创建dialog,他不会附属于任何的Activity,可以使用dialog的setOwnerActivity(Activity)来设置。
当想显示一个dialog的时候,调用showDialog(int),并传递一个整数来唯一标识想要显示的dialog即可。
当第一次使用一个dialog的时候,Android会调用onCreateDialog(int),应该在这里创建dialog。这个回调方法的参数是你传递给showDialog(int)的id。创建Dialog结束后,返回这个dialog对象。
在现实一个dialog之前,Android会先用可选的回调非法onPrepareDialog(int)。如果想在每次打开dialog的时候改变它的属性就可以通过重写这个方法来实现。他会在每次打开dialog的时候被调用,而onCreateDialog(int)只在第一次打开一个dialog的时候调用。如果不定义onPrepareDialog(),那么dialog将保持塌方上次被打开的属性。This method is also passed the dialog's ID, along with the Dialog object you created in onCreateDialog()。

创建Dialog的Example:
static final int DIALOG_PAUSED_ID = 0;
static final int DIALOG_GAMEOVER_ID = 1;
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;
}

显示创建的dialog:showDialog(DIALOG_PAUSED_ID);

Dismissing a Dialog

当准备关闭dialog的时候,可以调用dialogobject的dismiss()方法。如果必要,可以调用Activity的dismissDialog(int),它会有效的调用dialog的dismiss()。
如果你使用onCreateDialog(int)来管理dialog的状态,那么每次关闭dialog的时候,dialog的状态都会被Activity保存。如果这个dialog不会再次被使用或者清除它的状态很重要,可以调用removeDialog(int),这回清除内部任何对它的引用,如果它正在显示,会被关闭。

Using dismiss listeners

如果想在dialog被关闭的时候执行一些操作,应该为dialog设置on-dismiss listener。

首先定义DialogInterfacev.OnDismissListener接口。这个接口只有一个方法,vonDismiss(DialogInterface),当关闭dialog的时候会调用这个方法。把OnDismissListener的实现传递给setOnDismissListener()就可以了。
注意,dialog可以被取消。这是由用户显示取消dialog的特殊情况,在用户点击back键或者显示调用cancel()函数就是这种情况。当dialog被cancel的时候,OnDismissListener不会被通知,如果你想知道dialog被显示的cancel(不是正常的关闭),那么你应该用setOnCancel()注册一个vDialogInterface.OnCancelListener。

Creating an AlertDialog
AlertDialog是Dialog的子类,它可以构造用户交互的多数dialog,是建议的dialog类型。下列情况可以使用AldertDialog:
A tittle;
A text message;
One,two,or three buttons;
A list of selectable items (with optional checkboxes or radio buttons)

可以使用AlertDialog.Builder的子类来创建AlertDialog。通过AlertDialogBuilder(Context)来创建一个builder并使用这个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.

Adding buttons
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();

注意:每种类型的按钮只能添加一个。这限制了按钮的个数(3个)。这些名字于其功能无关只用来帮助你记住这些按钮是干嘛的。

Adding a list

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();

Adding checkboxes and radio buttons

可以用vsetMultiChoiceitems()和setSingleChoiceItems()来在AlertDialog上设置多选或者单选列表。如果使用onCreateDialog()创建这些选项列表,那么Android会维护这个选项列表的状态。在Activity存活期间,这个dialog会记住先前的选项状态,但是当用户利卡这个Activity时,选项状态就会丢失。
注意:为了在用户退出或者暂停这个Activity时保存选项状态,你必须在整个Activity的生命周期中恰当的保存和恢复选项状态。为了永久的保存选项状态(甚至在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():

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();

在setSingleChoiceItems()的第二个参数是一个整形表示哪个item被选中,item的标识从0开始,-1表示没有item默认被选中。

Creating a ProgressDialog
ProgressDialog是AlertDialog的子类,可以显示进度动画:用旋转的环表示进度未定义的task;用进度条表示定义了进度的task。这个dialog也可以提供按钮,比如下载过程中的取消按钮。
打开一个进度dialog简单到只要调用ProgressDialog.show()就可以了。
比如:
ProgressDialog dialog = ProgressDialog.show(MyActivity.this, "",
"Loading. Please wait...", true);
第一个参数是程序的context, 第二个参数是tittle,第三个是message, 第三个参数表示这个progress时候是不清楚的(它只在创建进度条的时候有意义, which is discussed in the next section).

Showing a progress bar
显示一个用动画表示进度的进度条:
1、用构造方法ProgressDialog(Context)初始一个ProgressDialog。
2、用setProgressStyle(int)设置style为STYLE_HORIZONTAL,并设置其他的属性,比如message等。
3、准备显示dialog的时候调用show()或者使用onCreateDialog(int)来返回这个ProgressDialog。
4、可以调用setProgress(int)传递目前完成的全部百分比或者vincrementProgressBy(int)传递增量值来增加进度条显示的进度。

比如:
ProgressDialog progressDialog;
progressDialog = new ProgressDialog(mContext);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMessage("Loading...");
progressDialog.setCancelable(false);

设置ProgressDialog很简单,创建ProgressDialog的多数代码多数是用来更新它的。你会发现需要使用Handler来创建新的线程来进行这项工作并把进度反映到Activity的UI上。

Example ProgressDialog with a second thread
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.

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...");
progressThread = new ProgressThread(handler);
progressThread.start();
return progressDialog;
default:
return null;
}
}

// Define the Handler that receives messages from the thread and update the progress
final Handler handler = new Handler() {
public void handleMessage(Message msg) {
int total = msg.getData().getInt("total");
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();
Bundle b = new Bundle();
b.putInt("total", total);
msg.setData(b);
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

如果需要一个自定义设计的dialog,你可以创建自己的layout。定义好layout后,传递root View对象或者leyout资源ID给setContentView(View)。
例如:
1、创建XML layout:
<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>
2、设置这个layout为这个dialog的内容    并定义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);
在初始化dialog后,使用setContentView(int)设置自定义的layout为dialog的content view。这时这个dialog已经定义好了layout,你可以使用dialog的findViewByID(int)来从layout中得到View对象,并修改其中的内容。
3、好了,现在你可以显示这个dialog了。

通过基类Dialog创建的dialog的必须有tittle,如果不调用setTittle(),那么tittle的空间会空着而且可见。如果你根本不需要tittle,那么应该使用AlertDialog来创建自定义的dialog。因为使用AlertDialog.Builder来创建AlertDialog更容易,你也不需要访问vsetContentView(int)而是用setView(View)。它接受一个View对象参数,所以你需要inflate the layout's root View object from XML。
To inflate the XML layout,使用getLayoutInflater()或者getSystemService()得到LayoutInflater,然后调用inflate(int, ViewGroup)(第一个参数是layout的资源ID,第二个是root view的标识符)。至此,你可以用这个inflated layout来得到layout中的View对象并且定义其中的内容了。然后初始AlertDialog.Builder并setView为这个inflated layout。
例如:
AlertDialog.Builder builder;
AlertDialog alertDialog;

Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER);
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.


来自: http://hi.baidu.com/leran0222/blog/item/b5395a4b908628fa83025c7e.html

原创粉丝点击