Android 自定义 dialog

Android 自定义 dialog

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值