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)佈局檔案,我們建立一個設定名稱的佈局檔案:
- <?xmlversion="1.0"encoding="utf-8"?>
-
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content">
- <TextView
- android:id="@+id/id_label_your_name"
- android:layout_width="wrap_content"
- android:layout_height="32dp"
- android:gravity="center_vertical"
-
android:text="Your name:"
- <EditText
- android:id="@+id/id_txt_your_name"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_toRightOf="@id/id_label_your_name"
- android:imeOptions="actionDone"
- android:inputType="text"/>
- <Button
- android:id="@+id/id_sure_edit_name"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true"
- android:layout_below="@id/id_txt_your_name"
- android:text="ok"/>
- </RelativeLayout>
b)繼承DialogFragment,重寫onCreateView方法
- package com.example.zhy_dialogfragment;
- import android.app.DialogFragment;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- publicclass EditNameDialogFragment extends DialogFragment
- {
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState)
- {
- View view = inflater.inflate(R.layout.fragment_edit_name, container);
- return view;
- }
- }
c)測試執行:
Main方法中呼叫:
- publicvoid showEditDialog(View view)
- {
- EditNameDialogFragment editNameDialog = new EditNameDialogFragment();
- editNameDialog.show(getFragmentManager(), "EditNameDialog");
- }
- publicclass EditNameDialogFragment extends DialogFragment
- {
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState)
- {
- getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
- View view = inflater.inflate(R.layout.fragment_edit_name, container);
- return view;
- }
- }
效果圖:
很完美的去掉了討厭的標題。
4、 重寫onCreateDialog建立Dialog
在onCreateDialog中一般可以使用AlertDialog或者Dialog建立對話方塊,不過既然google不推薦直接使用Dialog,我們就使用AlertDialog來建立一個登入的對話方塊。a)佈局檔案
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:orientation="vertical">
- <ImageView
- android:layout_width="match_parent"
- android:layout_height="64dp"
- android:background="#FFFFBB33"
- android:contentDescription="@string/app_name"
- android:scaleType="center"
- android:src="@drawable/title"/>
- <EditText
- android:id="@+id/id_txt_username"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginBottom="4dp"
- android:layout_marginLeft="4dp"
- android:layout_marginRight="4dp"
- android:layout_marginTop="16dp"
- android:hint="input username"
- android:inputType="textEmailAddress"/>
- <EditText
- android:id="@+id/id_txt_password"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginBottom="16dp"
-
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