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

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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值