1. 程式人生 > >Android 官方推薦 : DialogFragment 建立對話方塊

Android 官方推薦 : DialogFragment 建立對話方塊

1、 概述
DialogFragment在android 3.0時被引入。是一種特殊的Fragment,用於在Activity的內容之上展示一個模態的對話方塊。典型的用於:展示警告框,輸入框,確認框等等。
在DialogFragment產生之前,我們建立對話方塊:一般採用AlertDialog和Dialog。注:官方不推薦直接使用Dialog建立對話方塊。
2、 好處與用法
使用DialogFragment來管理對話方塊,當旋轉螢幕和按下後退鍵時可以更好的管理其宣告週期,它和Fragment有著基本一致的宣告週期。且DialogFragment也允許開發者把Dialog作為內嵌的元件進行重用,類似Fragment(可以在大螢幕和小螢幕顯示出不同的效果)。上面會通過例子展示這些好處~

使用DialogFragment至少需要實現onCreateView或者onCreateDIalog方法。onCreateView即使用定義的xml佈局檔案展示Dialog。onCreateDialog即利用AlertDialog或者Dialog創建出Dialog。

3、 重寫onCreateView建立Dialog

a)佈局檔案,我們建立一個設定名稱的佈局檔案:

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="wrap_content"
  4.     android:layout_height="wrap_content">
  5.     <TextView
  6.         android:id="@+id/id_label_your_name"
  7.         android:layout_width="wrap_content"
  8.         android:layout_height="32dp"
  9.         android:gravity="center_vertical"
  10.         android:text="Your name:"
    />
  11.     <EditText
  12.         android:id="@+id/id_txt_your_name"
  13.         android:layout_width="match_parent"
  14.         android:layout_height="wrap_content"
  15.         android:layout_toRightOf="@id/id_label_your_name"
  16.         android:imeOptions="actionDone"
  17.         android:inputType="text"/>
  18.     <Button
  19.         android:id="@+id/id_sure_edit_name"
  20.         android:layout_width="wrap_content"
  21.         android:layout_height="wrap_content"
  22.         android:layout_alignParentRight="true"
  23.         android:layout_below="@id/id_txt_your_name"
  24.         android:text="ok"/>
  25. </RelativeLayout>

b)繼承DialogFragment,重寫onCreateView方法
  1. package com.example.zhy_dialogfragment;  
  2. import android.app.DialogFragment;  
  3. import android.os.Bundle;  
  4. import android.view.LayoutInflater;  
  5. import android.view.View;  
  6. import android.view.ViewGroup;  
  7. publicclass EditNameDialogFragment extends DialogFragment  
  8. {  
  9.     @Override
  10.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  11.             Bundle savedInstanceState)  
  12.     {  
  13.         View view = inflater.inflate(R.layout.fragment_edit_name, container);  
  14.         return view;  
  15.     }  
  16. }  

c)測試執行:

Main方法中呼叫:

  1. publicvoid showEditDialog(View view)  
  2.     {  
  3.         EditNameDialogFragment editNameDialog = new EditNameDialogFragment();  
  4.         editNameDialog.show(getFragmentManager(), "EditNameDialog");  
  5.     }  
效果圖: 可以看到,對話方塊成功建立並顯示出來,不過預設對話方塊有個討厭的標題,我們怎麼去掉呢:可以在onCreateView中呼叫getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);即可去掉。即:
  1. publicclass EditNameDialogFragment extends DialogFragment  
  2. {  
  3.     @Override
  4.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  5.             Bundle savedInstanceState)  
  6.     {  
  7.         getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);  
  8.         View view = inflater.inflate(R.layout.fragment_edit_name, container);  
  9.         return view;  
  10.     }  
  11. }  

效果圖:
很完美的去掉了討厭的標題。
4、 重寫onCreateDialog建立Dialog
在onCreateDialog中一般可以使用AlertDialog或者Dialog建立對話方塊,不過既然google不推薦直接使用Dialog,我們就使用AlertDialog來建立一個登入的對話方塊。

a)佈局檔案

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="wrap_content"
  4.     android:layout_height="wrap_content"
  5.     android:orientation="vertical">
  6.     <ImageView
  7.         android:layout_width="match_parent"
  8.         android:layout_height="64dp"
  9.         android:background="#FFFFBB33"
  10.         android:contentDescription="@string/app_name"
  11.         android:scaleType="center"
  12.         android:src="@drawable/title"/>
  13.     <EditText
  14.         android:id="@+id/id_txt_username"
  15.         android:layout_width="match_parent"
  16.         android:layout_height="wrap_content"
  17.         android:layout_marginBottom="4dp"
  18.         android:layout_marginLeft="4dp"
  19.         android:layout_marginRight="4dp"
  20.         android:layout_marginTop="16dp"
  21.         android:hint="input username"
  22.         android:inputType="textEmailAddress"/>
  23.     <EditText
  24.         android:id="@+id/id_txt_password"
  25.         android:layout_width="match_parent"
  26.         android:layout_height="wrap_content"
  27.         android:layout_marginBottom="16dp"
  28.         android:layout_marginLeft

    相關推薦

    android開發 -- 對話方塊 Dialog 和 DialogFragment 詳解( Android 官方推薦 DialogFragment 建立對話方塊

     Android 官方推薦使用 : DialogFragment 建立對話方塊 ,不推薦直接使用Dialog建立對話方塊,所以能用寫對話方塊儘量用DialogFragment。自定義對話方塊也方便很多 推薦一篇DialogFragment的文章:http://blog.csdn.n

    Android 官方推薦 : DialogFragment 建立對話方塊

    1、 概述 DialogFragment在android 3.0時被引入。是一種特殊的Fragment,用於在Activity的內容之上展示一個模態的對話方塊。典型的用於:展示警告框,輸入框,確認框等等。 在DialogFragment產生之前,我們建立對話方

    android學習之使用AlertDialog建立對話方塊

    package com.example.learn.emptyactivity; import android.app.AlertDialog; import android.content.DialogInterface; import android.support.

    Android 繼承DialogFragment實現對話方塊

    前言 在重構專案UI時,由於需要重新改下對話方塊介面,然後期望效果圖如下: 雖然簡單,但是感覺很久都沒動手寫UI,差不多都忘了[尷尬],所以搞起來也是稍微耗了點時間,於是打算記錄下。 問題 繼承DialogFragment後,如果什麼都不處理,則效果

    android:將activity設定成對話方塊模式

    方法有兩個: 第一種是activity繼承的是Activity public class FirstActivity extends Activity 這時候需要在Androidmanifest.xml中為<activity>標籤新增新屬性 <activity

    Android 元件之DialogFragment建立小述

    一、概述 本節主要簡單介紹下DialogFragment的使用,DialogFragment是Fragment的子類,跟其他Fragment一樣,DialogFragment例項也是由託管Activity的FragmentManager管理著的,DialogFragment可通過呼叫

    android】IOS風格的對話方塊

    English|中文 一個高仿ios的文字、提示、item、grid的全域性和區域性對話方塊 該對話方塊專案基於Databinding和Recyclerview的全域性對話方塊是為了解決原生對話方塊在某些手機上面不支援展示的問題,區域性對話方塊是順便一起寫的,全域性對話方塊就算是

    建立對話方塊

    1.建立視窗1 開啟pycharm,開啟Qt Designer,建立視窗   修改主視窗大小和名字等   使用容器佈局,在新增一個button控制元件 2.建立視窗2 繼續建立一個視窗  3

    解決vc2017不能建立對話方塊類的問題

    我們在2017中建立對話方塊以後,不能建立類,並且彈出下面的對話方塊 根據上面的提示,我們來到上面的目錄,如下: 果真沒有simple目錄,那麼我們就拷貝一個進來,問題解決。這我將這個sample資料夾以附件形式放在這裡。 https://download.csdn.net/downlo

    Android官方推薦的集合類

    SparseArray SparseArray是android裡為<Interger,Object>這樣的Hashmap而專門寫的類,目的是提高記憶體效率,即如果你想使用Map<Integer,Object>則可以用SparseArray<Object&g

    EasyUI 動態建立對話方塊Dialog

    // 拒絕審批通過 function rejectApproval() { // 建立填寫審批意見對話方塊 $("<div id='reject-comment'> </div>").dialog({ ti

    MFC,線上程中建立對話方塊

    UINT CTestDlgDlg::ThreadFunc(PVOID pv){ //模態對話方塊//  CAboutDlg dlg;//  dlg.DoModal(); //非模態對話方塊  CTestDlgDlg *Testdlg = (CTestDlgDlg*)pv;   CDlgThread *dlg

    17. android dialog —— 單選列表對話方塊

    設定單選列表只需 AlertDialog.Builder裡面的setSingleChoiceItems 來設定即可 實現步驟如下: 第一步:用來顯示列表內容的 res/values/array.xml  <?xml version="1.0" encoding="ut

    Android 自定義彈出對話方塊顯示不全的問題

    昨天需要用自定義AlertDialog來實現一項功能,步驟大體是: 1.自定義佈局檔案 2.初始化AlertDialog,並設定setView 3.按鈕 文字框啥的處理 為了偷懶直接copy工程裡面同事完成的對話方塊。但是我修改後效果卻不一樣,先看我的佈局檔案圖示: 這

    MFC 動態建立對話方塊和控制元件

             一段時間以來,都在思考不採用Qt,用Win32-API或者MFC實現的程式使介面可配置。即先配置好需要什麼對話方塊、包含什麼控制元件、完成什麼任務,然後執行時建立,用以實現介面和功能的分離。需要可以動態配置控制元件建立、訊息接收函式。之前準備的本文

    android 如何在自定義對話方塊中獲取edittext中的資料

    在專案中忽然遇到這樣的問題,需要自定義對話方塊,對話方塊需要有一個輸入框,以便修改所選中的價格,然後點選確定之後,修改所顯示的價格。遇到的最大的問題就是如何能夠獲取到自定義對話方塊當中edittext輸入的數值,百度了很久,看到的答案都是如下: //得到自定義對話方塊

    VS2013/MFC程式設計入門之七(對話方塊建立對話方塊類和新增控制元件變數)

    前兩講中為大家講解了如何建立對話方塊資源。建立好對話方塊資源後要做的就是生成對話方塊類了。再宣告下,生成對話方塊類主要包括新建對話方塊類、新增控制元件變數和控制元件的訊息處理函式等。        因為給大家的例程Addition是基於對話方塊的程式,所以程式自動建立了

    Android webview監聽網頁對話方塊點選事件

    android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an appli 現在專案遇到這樣一個問題,HTML5網頁彈出一個Alert對話方塊,該文

    android service中彈出dialog對話方塊

    我們都知道在Activity中彈出Dialog很正常,因為Dialog是基於Window彈出。 但是我們經常會遇到這樣的需求,需要在service 中彈出一個Dialog對話方塊,做法有兩種 第一種,使用Activity,theme設定成 Dialog 並新增: inte

    Android快速高斯模糊對話方塊

    // MainActivity.java package com.example.blurdemo;import android.annotation.SuppressLint;import android.app.Activity;import android.conte