您的位置:首页 > 移动开发 > Android开发

Android中AlertDialog控件的基本使用和定制方法

2014-03-04 15:51 471 查看
Android中的许多组建都是可以由开发者自己定制的,通过自己定制,我们可以自己搭配或设计出一些漂亮的外观。当然AlertDialog也不例外。

AlertDialog显示的时候有三个部分组成,分别是:

1. 标题 (通过setTitle()方法来设置)

2. 内容 (通过setMessage()方法来设置)

3. 按钮 (通过setButton()、setPositiveButton()、setNegativeButton()等方法来设置)

三个部分在弹出的对话框中的位置如图(Android版本不同可能显示在不同):



1->标题(title)   2->内容(message)   3->按钮(buttons)

其中这三个部分里只有内容部分是可以定制的,也就是只有中间的那一片可以由我们自己设计并定制,当然,像按钮之类的东东可以自己在定制界面里加上而不用setButton()之类原有的。

那么我们现在先看看如何使用AlertDialog。AlertDialog要通过AlertDialog.Builder这个类来生成,如果只是像简单的弹出对话框的话下面的这段代码就可以:

AlertDialog dialog = new AlertDialog.Builder(this).create();
dialog.setTitle("对话框的标题");
dialog.setMessage("对话框的内容");
dialog.setButton(dialog.BUTTON_POSITIVE, "对话框的按钮", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 这里是按钮的功能函数
}
});
dialog.show();


如果设置Android支持的最低API版本>=11,那么可以使用DialogFragment来组织AlertDialog,下面的代码是官方代码:

public class FireMissilesDialogFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.dialog_fire_missiles)
.setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// FIRE ZE MISSILES!
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Create the AlertDialog object and return it
return builder.create();
}
}

可以这样来使用这个对话框:

FireMissilesDialogFragment dlg = new FireMissilesDialogFragment();
dlg.show(getFragmentManager(), "随意");
//dlg.dismiss(); 用这个来消失
如果是直接使用AlertDialog的话,有show()方法可以用来弹出对话框,但是如果使用DialogFragment的话show()方法需要含有参数,第一个参数是一个FragmentManager类型的对象,可以通过 getSupportFragmentManager() 或 getFragmentManager() 来获取。第二个参数是一个字符串,可以在之后的一些地方用来获取这个Fragment。

如果想要关闭对话框的话要通过使用dismiss()方法,还有另一个cancel()方法。他们的区别就在于:调用dismiss()方法的时候不会调用onCancel()生命周期函数,但是如果调用cancel()会调用onDismiss()方法和onCancel()方法。

接下来就是如何定制了,其实就是通过AlertDialog对象或AlertDialog.Builder对象的setView()方法来设置自己的内容布局,可以将定制的东西写成xml(放在/res/layout中),下面是来自官方的实例代码:

用来定制外观的xml文件,文件名为dialog_signin.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:src="@drawable/header_logo"
android:layout_width="match_parent"
android:layout_height="64dp"
android:scaleType="center"
android:background="#FFFFBB33"
android:contentDescription="@string/app_name" />
<EditText
android:id="@+id/username"
android:inputType="textEmailAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="4dp"
android:hint="@string/username" />
<EditText
android:id="@+id/password"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="16dp"
android:fontFamily="sans-serif"
android:hint="@string/password"/>
</LinearLayout>


然后只需在代码中通过填充器填充传入xml文件的id即可:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.dialog_signin, null))
return builder.create();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Android 控件 界面 编程