【问题标题】:Android activity inside dialog对话框内的 Android 活动
【发布时间】:2009-12-18 05:07:37
【问题描述】:

我想在弹出屏幕中启动一项活动。有什么快速改变的建议吗?

new AlertDialog.Builder(SearchResults.this)
        .setTitle("Refine") 
        .setItems(/*catNames*/, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                /* User clicked so do some stuff */
                String catName = catNames[which];
                String categoryIds = subCats.get(catName);
            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                //do nothing just dispose
            }
        })
        .create().show();

【问题讨论】:

    标签: android dialog android-activity


    【解决方案1】:

    您也可以应用此主题,使您的活动看起来像一个对话框:

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

    【讨论】:

    • 有没有办法动态地做这件事?
    • 这似乎将其主题化为对话框,但不会使其显示为对话框。
    【解决方案2】:

    如果您只想在用户从对话框中选择项目时启动活动,您可以这样做:

    new AlertDialog.Builder(SearchResults.this)
                        .setTitle("Refine") 
                        .setItems(/*catNames*/, new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                        /* User clicked so do some stuff */
                                        String catName = catNames[which];
                                        String categoryIds = subCats.get(catName);
                                        Intent intent = new Intent(SearchResults.this,YourActivity.class);
                                        startActivity(intent);
                        }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int whichButton) {
                                        //do nothing just dispose
                                }
                        })
                        .create().show();
    

    在您的 onClick() 方法中,您创建一个意图并将其传递给 startActivity() 方法。

    【讨论】:

    • 我想使用意图作为对话框的视图。
    • 嗯,我在这里有点困惑。 Intent 是要执行的操作 - 它不是 View/ViewGroup 组件,因此不能用作对话框的布局。 Intent 可以执行的操作之一是启动新活动 - 所以也许您想为对话框设置自定义布局?是这样吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-10
    • 1970-01-01
    • 2016-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多