android Dialogs

1.引言

Dialog是对话框的基类,可以实现以下子类:

AlertDialog,DatePickerDialog,TimPickerDialog。

这些类为你定义了样式和结构,不过你可以使用DialogFragment作为对话框的内容。通过DialogFragment你可以自由控制你的对话框,而不是继承Dialog对象沿用Dialog对象的一些方法。

当用户按返回键或屏幕翻转的时候要注意DialogFragment的生命周期。DialogFragment也允许你把它的UI用在打的UI上。

如果使用Support library记得引用android.support.v4.app.DialogFragment而不是android.app.DialogFragment.

2.一个最简单的例子

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

当你想显示Dialog时,只需调用show()

3.方法回调

在DialogFragment中设置一个interface,通过这个接口可以将事件传递到主activity中

public class NoticeDialogFragment extends DialogFragment {
   
    /* The activity that creates an instance of this dialog fragment must
     * implement this interface in order to receive event callbacks.
     * Each method passes the DialogFragment in case the host needs to query it. */
    public interface NoticeDialogListener {
        public void onDialogPositiveClick(DialogFragment dialog);
        public void onDialogNegativeClick(DialogFragment dialog);
    }
   
    // Use this instance of the interface to deliver action events
    NoticeDialogListener mListener;
   
    // Override the Fragment.onAttach() method to instantiate the NoticeDialogListener
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        // Verify that the host activity implements the callback interface
        try {
            // Instantiate the NoticeDialogListener so we can send events to the host
            mListener = (NoticeDialogListener) activity;
        } catch (ClassCastException e) {
            // The activity doesn‘t implement the interface, throw exception
            throw new ClassCastException(activity.toString()
                    + " must implement NoticeDialogListener");
        }
    }
    ...
}

4.DialogFragmnt既可以显示为一个全屏的对话框,亦可以显示为一个嵌入的Fragment

public class CustomDialogFragment extends DialogFragment {
    /** The system calls this to get the DialogFragment‘s layout, regardless
        of whether it‘s being displayed as a dialog or an embedded fragment. */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // Inflate the layout to use as dialog or embedded fragment
        return inflater.inflate(R.layout.purchase_items, container, false);
    }
 
    /** The system calls this only when creating the layout in a dialog. */
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // The only reason you might override this method when using onCreateView() is
        // to modify any dialog characteristics. For example, the dialog includes a
        // title by default, but your custom layout might not need it. So here you can
        // remove the dialog title, but you must call the superclass to get the Dialog.
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        return dialog;
    }
}
public void showDialog() {
    FragmentManager fragmentManager = getSupportFragmentManager();
    CustomDialogFragment newFragment = new CustomDialogFragment();
   
    if (mIsLargeLayout) {
        // The device is using a large layout, so show the fragment as a dialog
        newFragment.show(fragmentManager, "dialog");
    } else {
        // The device is smaller, so show the fragment fullscreen
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        // For a little polish, specify a transition animation
        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        // To make it fullscreen, use the ‘content‘ root view as the container
        // for the fragment, which is always the root view for the activity
        transaction.add(android.R.id.content, newFragment)
                   .addToBackStack(null).commit();
    }
}

其中mIsLargeLayout根据当前设备判断是否使用app的大UI布局。

mIsLargeLayout的取值方法如下:

res/values/bools.xml

<!-- Default boolean values -->
<resources>
    <bool name="large_layout">false</bool>
</resources>

res/values-large/bools.xml

<!-- Large screen boolean values -->
<resources>
    <bool name="large_layout">true</bool>
</resources>
boolean mIsLargeLayout;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mIsLargeLayout = getResources().getBoolean(R.bool.large_layout);
}

5.如果你的应用只是适配了小屏幕的手机,那么:

<activity android:theme="@android:style/Theme.Holo.DialogWhenLarge" >

将你的activity设置为Theme.Holo.DialogWhenLarge会在大屏幕上展示为对话框。

android Dialogs

时间: 2024-10-13 13:34:34

android Dialogs的相关文章

Android Dialogs(1)Dialog简介及dialog种类

Dialogs A dialog is a small window that prompts the user to make a decision or enter additional information. A dialog does not fill the screen and is normally used for modal events that require users to take an action before they can proceed. Dialog

Android Dialogs(3)警示Dialog教程[创建,单选,复选,自定义]等等

Building an Alert Dialog The AlertDialog class allows you to build a variety of dialog designs and is often the only dialog class you'll need. As shown in figure 2, there are three regions of an alert dialog: Figure 2. The layout of a dialog. Title T

Delphi XE5 Android Dialogs 对话框(模拟做了一套)

最近要在Android中使用对话框, 但发现无现成的, TOpenDialog等已经不支持移动设备,还好系统提供了一些文件目录函数可用,于是简单的模拟了一个,支持OpenDialog ,SaveDialog, SelectDirectory, 以及文件过滤和多选. 截了一些图片,好像有些失真,还是以真机为准. OpenDialog : SaveDialog: SelectDirectory: 图片多选过滤: 工程下载:http://download.csdn.net/detail/flcop/6

Android Dialogs(2)最好用DialogFragment创建Dialog

Creating a Dialog Fragment You can accomplish a wide variety of dialog designs—including custom layouts and those described in theDialogs design guide—by extending DialogFragment and creating a AlertDialog in the onCreateDialog()callback method. For

wesome-android

awesome-android Introduction android libs from github System requirements Android Notice If the lib is no longer being maintained,please do not add it here. How To Contribute Step 1. Add a Item as follows: **Library Name**[one space]Short Description

GitHub中常用开源库

awesome-android Introduction android libs from github System requirements Android Notice If the lib is no longer being maintained,please do not add it here. Libs Table of contents Framework EventBus Orm Image Loading Animations Network Widget Materia

github中的常用库

awesome-android android libs from github Download ZIP Download TAR View On GitHub This project is maintained bysnowdream awesome-android Introduction android libs from github System requirements Android Notice If the lib is no longer being maintained

delphi for android 获取手机号

uses  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,Androidapi.JNI.GraphicsContentViewText,   Androidapi.JNIBridge,  Androidapi.JNI.Telephony, Androidapi

GitHub Top 100的Android开源库

本项目主要对目前 GitHub 上排名前 100 的 Android 开源库进行简单的介绍, 至于排名完全是根据GitHub搜索Java语言选择「Best Match」得到的结果,然后过滤了跟Android不相关的项目,所以排名并不具备任何官方效力,仅供参考学习,方便初学者快速了解当前一些流行的Android开源库. 1. React Native 这个是 Facebook 在 React.js Conf 2015 大会上推出的基于 JavaScript 的开源框架 React Native,