2013-07-12 30 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" />