Android中AlertDialog控件的基本使用和定制方法
来源:互联网 发布:淘宝店收费 编辑:程序博客网 时间:2024/05/13 10:11
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() {@Overridepublic 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即可:
@Overridepublic 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中AlertDialog控件的基本使用和定制方法
- Android 中定制AlertDialog
- Android中AlertDialog和Toast的使用
- Android中AlertDialog和Toast的使用
- Android中AlertDialog和Toast的使用
- Android中AlertDialog和Toast的使用
- Android中AlertDialog和ProgressDialog的使用
- Android中AlertDialog的使用
- Android中AlertDialog的使用
- Android 中 AlertDialog的使用
- Android中AlertDialog的使用
- android AlertDialog 对话框的基本使用
- AlertDialog的基本使用
- AlertDialog的基本使用
- Android中WebView控件的基本使用
- Android中AlertDialog使用
- Android中AlertDialog对话框的使用
- Android中AlertDialog的简单使用
- C++转C#重要方法记录
- 让员工更积极的六种关键因素
- 为MyEclipse添加自定义注释
- 转载_比较全面的gdb调试命令
- script:获取某张表上所有的DML操作
- Android中AlertDialog控件的基本使用和定制方法
- 昆明火车站“3·01”严重暴力恐怖案成功告破
- 深入 Java 调试体系: 第 1 部分,JPDA 体系概览
- Xcode5和ObjC新特性
- 文件透明加解密的实现层次
- Amazon前技术副总裁解剖完美技术面试
- tar 解压命令
- Android 资源名资源id互转,根据路径获取bitmap跟drawable
- Extjs树的各种玩法案例