2012-05-16 38 views
0

我有一个AlertDialog.Builder其中存在一个按钮,并且我想在点击该对话框时关闭对话框。取消使用AlertDialog.Builder创建的对话

但是没有.dismiss().cancel()方法。

LayoutInflater inf = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View layout = inf.inflate(R.layout.map_window, null); 

    window = new AlertDialog.Builder(activity); 
    window.setCancelable(true); 

    buttonStar = (ImageButton)layout.findViewById(R.id.buttonStar); 
    buttonStar.setBackgroundResource(R.drawable.star); 
    buttonStar.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View arg0) { 
     //finishing window; 
    } 
     }); 


    window.setPositiveButton("Volver", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int wichButton) { 

     } 
    }); 

    window.show(); 

} 
+0

在Dialog上肯定有'cancel()'方法。 –

+0

肯定有'cancel()'和'dismiss()'方法。 :)。 'dialog.dismiss()'或'dialog.cancel()'会做相应的功能。 – Niranjan

+0

window.dismiss和window.cancel不存在http://developer.android.com/reference/android/app/AlertDialog.Builder.html我需要一个对话框,作为两个人的回答 – user1256477

回答

4

AlertDialog.Builder#show返回AlertDialog本身,所以刚从show返回AlertDialog本身和调用它解雇:

AlertDialog dialog; 
//... 

dialog = window.show(); 
+0

完美!有效!!非常感谢! – user1256477

2

尝试从对话框对象取消它。创建对话框对象如下:

Dialog dialog = window.create(); 
dialog.cancel() 
+0

完美!有效!!非常感谢! – user1256477

+0

我可以接受两个答案吗? – user1256477

+0

@ user1256477 - 不,请根据您的喜好决定。 – MByD

0

找到AlertDialog的波纹管代码。使用新的DialogInterface.OnClickListener()而不是onClick()。

AlertDialog ad = new AlertDialog.Builder(MainCatalogueActivity.this) 

        .setTitle("Server Response Error") 

        .setMessage("some message ") 

        .setIcon(R.drawable.ic_menu_close_clear_cancel) 

        .setNegativeButton("Close", new DialogInterface.OnClickListener() { 

         @Override 
         public void onClick(DialogInterface dialog, int which) { 

          Intent intent = new Intent(Intent.ACTION_MAIN); 

          intent.addCategory(Intent.CATEGORY_HOME); 

          intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

          startActivity(intent); 

          finish(); 


         } 
        }).create(); 
    ad.show();