对话框内的Android活动

时间:2009-12-18 05:07:37

标签: android dialog android-activity

我想在弹出屏幕中启动一项活动。有没有快速变化的建议?

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

2 个答案:

答案 0 :(得分:22)

您也可以应用此主题,以便您的活动显示为对话框:

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

答案 1 :(得分:1)

如果你只想在用户从对话框中选择一个项目时开始活动,你可以这样做:

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()方法中,您创建一个intent并将其传递给startActivity()方法。