1. 程式人生 > >AlertDialog.Builder彈出自定義Layout視窗

AlertDialog.Builder彈出自定義Layout視窗

------------------------------------------------------------------------------------------------------

       此文章僅作為學習交流所用

       轉載或引用請務必註明原文地址:

       或聯絡作者:[email protected] 

       謝謝!                    

------------------------------------------------------------------------------------------------------


前面說過,使用AlertDialog.Builder彈出視窗,一般,是下面幾個步驟.

(一)建立AltrtDialog.Builder物件,該物件是AlterDialog的建立器

Public Constructors
(Context context)Constructor using a context for this builder and the  it creates.
(Context context, int theme)Constructor using a context and theme for this builder and the  it creates.

(二)呼叫 AltrtDialog.Builder的方法為對話方塊設定圖示,標題,內容等.

(三)呼叫 AltrtDialog.Builder的create()方法建立對話方塊

()Creates a  with the arguments supplied to this builder.

(四)呼叫 AltrtDialog.Builder的show()方法顯示對話方塊

show()Creates a  with the arguments supplied to this builder and 's the dialog

二.使用AlertDialog.Builder載入自定義View

按照上面的步驟,使用的是預設的AlertDialog.Builder的視窗顯示方式,如果想要顯示內容豐富的彈出視窗,如裡面有一些輸入框之類的,如下面的圖片所示,那麼,就需要我們使用AlertDialog.Builder.setView(View v)方法載入自定義的View來作為視窗的顯示方式了.

.

(一) 這裡自定義的佈局檔案為 order.xml.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffffff"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#ff000000"
        android:layout_marginTop="41dip"
        android:layout_marginBottom="41dip"
        android:layout_marginLeft="41dip"
        android:layout_marginRight="41dip"
        android:orientation="vertical" >
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="請輸入欲預約圖書的書號: "
            android:textSize="25dip" >
        </TextView>
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="41dip"
        android:layout_marginRight="41dip"
        android:layout_marginBottom="24dip"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="預約書號: "
            android:textColor="#000000"
            android:textSize="25dip" >
        </TextView>

        <EditText
            android:id="@+id/orderEditSH"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="10009"
            android:textColor="#000000"
            android:textSize="25dip" >
        </EditText>
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginLeft="41dip"
        android:layout_marginRight="41dip"
        android:layout_marginBottom="41dip"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/startOrder_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="開始預約圖書   "
            android:textSize="25dip" >
        </Button>

        <Button
            android:id="@+id/managerOrder_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="管理個人預約  "
            android:textSize="25dip" >
        </Button>
    </LinearLayout>

</LinearLayout>

(二)下面,我們通過程式碼,載入這個自定義View

    首先,獲取需要載入的佈局檔案order.xml, 這裡採用的是LayoutInflater,而不是我們平時使用的 findViewById( ).LayoutInflater的作用類似於 findViewById(),不同點是LayoutInflater是用來找layout資料夾下的xml佈局檔案,並且例項化!而 findViewById()是找具體某一個xml下的具體 widget控制元件(如:Button,TextView等)。使用LayoutInflater來獲取佈局檔案有三種方式:

第一種方式:

LayoutInflater inflater = LayoutInflater.from(this);  
View layout = inflater.inflate(R.layout.order, null); 
第二種方式:
LayoutInflater inflater = getLayoutInflater();  
View layout = inflater.inflate(R.layout.order, null);  
第三種方式:
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);  
View layout = inflater.inflate(R.layout.main, null);

以前我們在Activity裡面使用某個控制元件時,通常是直接使用findViewById()方法來獲取這個控制元件,而如果我們要使用AlertDialog.Builder顯示的自定義Layout裡面的控制元件,需要注意要顯示地用View物件呼叫findViewById()這個方法.如上面建立的是View layout物件.那麼,需要使用如下訪求獲取EditText  orderEditSH.

EditText orderBookNum=(EditText)layout.findViewById(R.id.orderEditSH);

使用AlertDialog.Builder彈出上面自定義視窗的完整程式碼如下:

LayoutInflater inflater = LayoutInflater.from(this);
View layout=inflater.inflate(R.layout.order,null);
		
AlertDialog.Builder builder =new AlertDialog.Builder(Order.this);
builder.setView(view);
builder.setCancelable(false);
builder.create().show();
Button startOrder_btn=(Button) layout.findViewById(R.id.startOrder_btn);
Button managerOrder_btn=(Button) layout.findViewById(R.id.managerOrder_btn);
EditText orderBookNum=(EditText)layout.findViewById(R.id.orderEditSH);
		
startOrder_btn.setOnClickListener(new OnClickListener(){
	public void onClick(View v) {
		//加上你要實現的程式碼		
		}
	});
managerOrder_btn.setOnClickListener(new OnClickListener(){
	public void onClick(View v) {//加上你要實現的程式碼
		}
	});


相關推薦

AlertDialog.Builder出自定義Layout視窗

------------------------------------------------------------------------------------------------------        此文章僅作為學習交流所用        轉載或引用

android實現百度地圖點選覆蓋物(MyLocationOverlay)出自定義視窗

一:增加覆蓋物MyLocationOverlay          MyLocationOverlay在普通的Overlay基礎上進行了封裝,可以更好的進行一個定位處理,例如方向。        要想點選MyLocationOverlay觸發一個事件,就需要繼承MyLoc

ABAP出自定義螢幕

 以前聽說彈出螢幕只能用控制元件在上面。當時也沒試。 結果在今天弄東西發現要用到自定義的螢幕,才試了下,也挺簡單的。 定義一個對話螢幕和對話方塊狀態。 然後在 CALL SCREEN 0301 STARTING AT 20 10. 在按鈕返回原螢幕時用 SET SCRE

百度地圖出自定義

在百度地圖基礎上 自定義了一個彈出框 實現一些文字的介紹 頁面的跳轉 比如調到導航之類的  final View popupView = LayoutInflater.from(context).inflate(R.layout.baidu_popwindow, nu

對div實現右鍵出自定義選單

實現只對藍色的Div彈出自定義選單,其他位置彈出預設的瀏覽器選單 js程式碼如下: <script type="text/javascript"> $(function(){ var html=''; html +='<div id

點選出自定義檢視

demo效果 這個效果比較簡單,直接記錄一下。 自定義一個繼承自UIView的檢視,定義兩個方法一個顯示方法,一個消失方法。 /** * 顯示屬性選擇檢視 * * @para

ArcGIS API for Silverlight 點選地圖出自定義窗體

下面是通過新建一個Silverlight的UserControl類,裡面自定義樣式,只要你能想到的,都可以做出來,然後在地圖中點選點,例項化該類即可,主要程式碼如下: <UserControl x:Class="MapClient.MapTip" xmln

關於AlertDialog.Builder犯的一個錯和自定義佈局的限制寬高一個知識點

先看錯誤程式碼 AlertDialog.Builder builder=new AlertDialog.Builder(getApplicationContext()); builder.setTitle("溫馨提示")

在基類layout定義視窗中增加定製的layout(巢狀layout?)

在基類中定義了模板視窗,在繼承類中通過layout檔案修改其中一個視窗的內容。 // 基類layout中定義了一個id為LCD的佈局 RelativeLayout mLCD = (RelativeLayout) findViewById(R.id.LCD);   // 基類

定義AlertDialog.Builder對話方塊

在工作中,原生態的AlertDialog已經遠遠不能滿足我們工作的需求,這時候,就需要使用自定義的AlertDialog.Builder: 在自定義之前,我們先了解下, **原生態的AlertDialog的基本使用** setTitle():設定對話方塊

【iOS】UICollectionView自己定義Layout之蜂窩布局

with top http reserve src 布局 step object .com 網上的UICollectionView的Layout布局,其cell的形狀多為矩形和圓形。 本篇博文將正六邊形作為cell的基本形狀,為您展現獨特的蜂窩布局效果及實現源代碼。 幫

java中異常處理機制 throw拋出自定義業務邏輯異常 throws繼續拋出 catch捕獲後會自動繼續拋向調用方法

異常處理機制 ... cep super finally sta exc ace 避免 package com.swift; public class Exception_TestC { public static void main(String[] arg

spring boot分環境導出自定義xml配置

springboot 分環境 xml配置背景介紹: 由於新的spring boot項目需要使用老的jar包,老的jar包的配置是用xml方式配置的,而且開發development、測試test、集成off、正式production環境都會有所不同,這種時候我們就需要讓spring boot 分環境加

定義WPF 視窗樣式

原文: 自定義WPF 視窗樣式 自定義 Window 在客戶端程式中,經常需要用到自定義一個 Window ,大部分是為了好看吧。做了很多研究和實踐之後,覺得需要把這個過程寫下來,以供查閱。 WPF 提供的豐富的功能使得自定義

Unity在Android 6.0及以上版本出許可權申請視窗的問題

Android 版本大於等於6.0(SDK版本大於等於23),許可權需要應用自主申請,動態申請許可權。 如果Unity做為Android的子模組,除了應用本身會有申請許可權的彈窗之外,Unity也會自動彈出許可權申請視窗。 網路許可權(普通許可權)、手機震動許

Android開發(AlertDialog對話方塊自定義佈局和多選列表不共存的替代辦法)

這個實現功能花了一點時間,當時忙了很晚,只怪當時沒有想出其他解決辦法。言歸正傳。 前幾天有這麼一個小夥伴,在開發有這樣的地圖app,該地圖app有多個地圖圖層,這些地圖圖層可提供給使用者操作,比如說圖層的顯示控制,以及選擇需要的圖層供查詢。由於該地圖app在主介面已經佈局很

點選文字或按鈕出一個DIV視窗(DIV懸浮視窗

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head>

關於獲取出的子視窗中datagrid選中的值

對於常見的一個普通視窗,可以用以下的方式進行彈出視窗和獲取選中的datagrid中的row function chooseXxx() { var dia = top.sy.iframeDialog({ id:'u_frame',

[Swift通天遁地]一、超級工具-(14)使用SweetAlert製作漂亮的自定義Alert視窗

本文將演示一款非常強大的警告視窗。 Github地址:【SweetAlert】 下載所需的第三方類庫。在下載的資料夾中,選擇: 【SweetAlert】->【SweetAlert.swift】拖動到專案中 ->在彈出的新增檔案視窗中,保持預設的設定選項->【Finish】 在專案導

禁止開機出160資訊視窗

(160資訊彈窗截圖) 一開機電腦就彈出上圖中的160資訊的彈窗出來,一開始還真有點莫名其妙,還以為中招了!細看下工作列160資訊視窗的文字提示,發現有個人生日曆的字樣,好吧,明白了,是人生日曆整的開機彈窗新聞。第一反應是去找關閉彈窗的設定項,竟意外發現人