0

我正在查看 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();
    }
}
4

2 回答 2

2

不确定这个具体的例子,但一般来说,为了让一个活动看起来像一个对话框,把主题放在你的清单中(在你的活动下):

<activity android:theme="@android:style/Theme.Dialog" />
于 2013-07-12T17:50:30.833 回答
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>
于 2013-07-12T17:50:55.227 回答