2012-04-23 184 views
1

我已經把一個ListView放到AlertDialog中,當我點擊ListView項目時我想關閉AlertDialog;我該怎麼做?如何關閉AlertDialog?

public void createSearchDialog(final String[] Memo){ 
    LayoutInflater factory = LayoutInflater.from(this); 
    View searchView = factory.inflate(R.layout.seach_dialog, null); 

    lv = (ListView) searchView.findViewById(R.id.search_list); 
    lv.setAdapter(new MyPerformanceArrayAdapter(this, Memo, memo_PW)); 
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     public void onItemClick(AdapterView<?> arg0, View view, int position,long arg3) { 
      // TODO Auto-generated method stub 

        /// close dialog 
     } 
    }); 

    searchBuilder = new AlertDialog.Builder(this); 
    searchBuilder.setTitle("Search") 
      .setView(searchView) 
      .setNegativeButton("Back", new DialogInterface.OnClickListener() {    
      public void onClick(DialogInterface arg0, int arg1) { 
       // TODO Auto-generated method stub 

      } 
     }) 
      .show();  
} 
+5

處理檢查Listview.setOnItemClickListener()和dialog.dismiss() – 2012-04-23 12:48:52

回答

1

而從使用應用程序的上下文,你可以解僱是通過調用它的dimiss。

Application.getProgressDialog().dismiss(); 

這已經在列表視圖的onItemClick()

+0

我可以'找到解僱功能! – Tunandroid 2012-04-23 12:55:03

+0

「將可取消性設置爲true:'Dialog.setCancelable(false);'」... 什麼?你並不是偶然意味着'Dialog.setCancelable(true);' – Jave 2012-04-23 12:55:21

+0

對不起,當你想要點擊後退鍵時,對話框被取消。 – 2012-04-23 12:58:25

1

隨便抓的一個指針alertdialog這樣

AlertDialog myDialog = searchBuilder.setTitle("Search") 
      .setView(searchView) 
      .setNegativeButton("Back", new DialogInterface.OnClickListener() {    
      public void onClick(DialogInterface arg0, int arg1) { 
       // TODO Auto-generated method stub 

      } 
     }).create(); 

再後來,你可以關閉致電

myDialog.dismiss() 
+0

,但我不想在我的搜索AlertDialog界面添加一個按鈕,我該怎麼辦? – Tunandroid 2012-04-23 13:53:44

+0

@ user1351316您不添加按鈕,只是添加一個名爲myDialog的alertdialog的引用。然後將此參考傳遞給您的列表。在列表中調用第二行代碼,應該像魅力一樣工作。 – MikeIsrael 2012-04-23 13:58:39

+0

@ user1351316你明白嗎?讓它工作? – MikeIsrael 2012-04-23 18:03:06