【问题标题】:How does the Android life cycle demo make a dialogue box?Android生命周期demo如何制作对话框?
【发布时间】:2013-07-12 17:32:56
【问题描述】:

我正在查看 android 开发网站 (http://developer.android.com/training/basics/activity-lifecycle/index.html) 上提供的生命周期演示。单击暂停按钮时会出现一个对话框,但我无法弄清楚它在代码中的哪个位置将对话活动变为对话框而不是正常活动。我试图在我自己的应用程序中实现这一点,以便我可以尝试暂停,但我只是不明白对话框来自哪里。使活动显示为对话框的代码在哪里?

这是用户界面的代码

<?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();
    }
}

【问题讨论】:

  • 我看不到您指的是什么,您可以发布指向您正在谈论的代码的链接吗?

标签: android lifecycle


【解决方案1】:

不确定这个具体示例,但一般来说,要使活动看起来像一个对话框,请将主题放在清单中(在您的活动下):

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

【讨论】:

    【解决方案2】:

    单击按钮时会出现对话框。处理方法是:

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

    此处理程序在Activity*.java 文件中定义。

    此处理程序通过activity_*.xml 文件中的onClick 属性映射到按钮:

       <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"
            />
    

    此属性指定在用户单击按钮时应调用 Activity 上的哪个函数。

    DialogActivity 启动时,它会从activity_dialog.xml 文件中加载其布局。

    正如@RSenApps 指出的那样,要使活动看起来像一个对话框,您需要在AndroidManifest.xml 中指定主题

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

    【讨论】:

    • 是的,我明白你所说的,但我不明白的是它说单击按钮时调用的活动的布局应该是对话框而不是任何其他布局.
    • 是的,我想我在你评论的同时也在编辑 :-)
    • 哦!它在清单中!谢谢!我花了至少一个小时试图弄清楚这一点。稍后我会看看这是否能解决我的问题。
    • 第二个问题,为什么出现对话时activity会暂停?
    • 简短的回答是它暂停,因为它不再是顶级活动,但仍然部分可见。更长的简短答案是当一个活动不再处于活动状态时(在顶部),它应该释放它不再需要的资源(如 GPS 或其他东西),以便新的顶级活动可以使用它们。然后在恢复活动时重新获取这些资源。如果新活动完全隐藏,则旧活动将停止并可能从内存中删除。由于它仍然部分可见,因此无法停止和删除它,因为这对用户来说看起来很奇怪。
    猜你喜欢
    • 2016-07-03
    • 1970-01-01
    • 2012-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-14
    • 2013-04-06
    • 1970-01-01
    相关资源
    最近更新 更多