9

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

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

2 回答 2

22

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

<activity android:theme="@android:style/Theme.Dialog">
于 2009-12-19T00:12:04.747 回答
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() 方法中,您创建一个意图并将其传递给 startActivity() 方法。

于 2009-12-18T09:56:34.183 回答