2013-07-07 93 views
0

在android編程中,我想打開一個標題爲「Login」的彈出框。 內容應該是這樣的:如何打開對話框並使用佈局顯示它?

Login

Username 
[________] (input field) 

Password 
[________] (password field) 

[Cancel] [Login] 

但我要表明,該使用佈局文件。我不想以編程方式添加所有這些。

任何人都可以告訴我一個如何做到這一點的例子嗎?

謝謝

+0

使用彈出窗口來實現它。 –

回答

0

這裏有你需要的步驟如下:

- = - 創建一個xml佈局文件(比如「my_popup_window.xml」)。使用您所提供的信息,這可以是: -

<LinearLayout(Vertical)> 

    <TextView("Login") /> 

    <TextView("Username") /> 

    <EditText /> 

    <TextView("Password") /> 

    <EditText /> 

    <LinearLayout> 

     <Button("Cancel") /> 

     <Button("Login") /> 

    </LinearLayout> 

</LinearLayout> 

- = - 在你的活動中,創建一個方法 「showPopupWindow()」:

void showPopupWindow() { 
    // inflate your layout 
    View myPopupView = getLayoutInflater().inflate(R.layout.my_popup_window, null); 

    // Create the popup window; decide on the layout parameters 
    PopupWindow myPopupWindow = new PopupWindow(myPopupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 

    // find and initialize your TextView(s), EditText(s) and Button(s); setup their behavior 

    // display your popup window 
    myPopupWindow.showAtLocation(myPopupView, Gravity.CENTER, 0, 0); 
} 

電話時,需要出示此此方法彈出窗口。