2013-07-12 33 views
0

我期待在提供的Android開發人員網站(http://developer.android.com/training/basics/activity-lifecycle/index.html)在生命週期的演示。當單擊暫停按鈕時會出現一個對話框,但我無法弄清楚代碼中將對話活動置於對話框中的哪個位置,而不是正常的活動。我試圖在自己的應用程序中實現這一點,以便我可以嘗試暫停,但我不明白對話框的來源。用於使活動顯示爲對話框的代碼在哪裏?Android生命週期演示如何創建對話框?

下面是UI

<?xml version="1.0" encoding="utf-8"?> 
<!-- 
Copyright (C) 2012 The Android Open Source Project 

Licensed under the Apache License, Version 2.0 (the "License"); 
you may not use this file except in compliance with the License. 
You may obtain a copy of the License at 

    http://www.apache.org/licenses/LICENSE-2.0 

Unless required by applicable law or agreed to in writing, software 
distributed under the License is distributed on an "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
See the License for the specific language governing permissions and 
limitations under the License. 
--> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="225dp" 
    android:layout_height="120dp" 
    android:background="@color/dark_yellow" 
    android:padding="12dip" 
    > 

<TextView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/dialog_text" 
    android:gravity="center_horizontal" 
    android:textSize="@dimen/font_medium" 
    android:textColor="@color/light_yellow" 
    android:paddingBottom="12dip" 
    /> 

<Button 
    android:id="@+id/btn_finish_dialog" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:text="@string/btn_finish_dialog_label" 
    android:layout_gravity="center_horizontal" 
    android:onClick="finishDialog" 
    /> 
</LinearLayout> 

此處的代碼用於與UI

/* *版權(C)2012的Android開源項目 * 相關聯的類的代碼*根據Apache許可證2.0版(「許可證」)獲得許可; *除遵守許可證外,您不得使用此文件。 *您可以在獲得許可證的副本 * * http://www.apache.org/licenses/LICENSE-2.0 * *除非適用法律要求或書面同意,根據許可證分發的軟件 *分佈在「原樣」的基礎, *沒有任何形式的保證或條件,無論是明示還是暗示。 *請參閱許可證以瞭解許可證下的特定語言管理權限和 *限制。 */

package com.example.android.lifecycle; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.Window; 

public class DialogActivity extends Activity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.activity_dialog); 
    } 

    /** 
    * Callback method defined by the View 
    * @param v 
    */ 
    public void finishDialog(View v) { 
     DialogActivity.this.finish(); 
    } 
} 
+0

我不明白你指的是你能張貼的鏈接代碼到指定的主題談論? – Rarw

回答

0

的單擊按鈕時會出現對話框。該處理方法是:

public void startDialog(View v) { 
    Intent intent = new Intent(ActivityA.this, DialogActivity.class); 
    startActivity(intent); 
} 

這個處理器在Activity*.java文件中定義。

這個處理器是通過onClick屬性映射到該按鈕在activity_*.xml文件:

<Button 
     android:id="@+id/btn_start_dialog" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:text="@string/btn_start_dialog_label" 
     android:layout_toRightOf="@id/btn_finish_a" 
     android:onClick="startDialog" 
     /> 

該屬性指定的活動有什麼功能,應在用戶點擊按鈕時調用。

DialogActivity啓動時,它從activity_dialog.xml文件加載它的佈局。

由於@RSenApps指出的,使活動看起來像一個對話框,你需要在AndroidManifest.xml

<activity android:name=".DialogActivity" 
       android:theme="@android:style/Theme.Dialog"> 
    </activity> 
+0

是的,我明白你說什麼,但我不明白的是,它說的是,當按鈕被點擊應該是一個對話框,而不是任何其他佈局活動的佈局調用。 – michaelAdam

+0

是的,我想我是在你評論:-) –

+0

OH同時編輯!它在清單中!謝謝!我花了至少一個小時試圖弄清楚這一點。我會看看這會在以後解決我的問題。 – michaelAdam

2

不知道這個具體的例子,但在一般情況下,使活動看起來像一個對話框,將在您的清單主題(您的活動下):

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