单击gridview时关闭alertdialog

时间:2014-04-29 17:59:21

标签: android

gridView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View v,
                int position, long id) {

            AlertDialog.Builder builder;
            Context mContext = getActivity();
            LayoutInflater inflater = (LayoutInflater) mContext
                    .getSystemService(getActivity().LAYOUT_INFLATER_SERVICE);
            View layout = inflater
                    .inflate(R.layout.alertdiaolog_main, null);
            GridView gridView2 = (GridView) layout
                    .findViewById(R.id.alert_gridview);

            gridView2.setAdapter(new AlertImageAdapter(getActivity()));
            gridView2.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View v,
                        int position, long id) {  

                }
            });

            builder = new AlertDialog.Builder(mContext);
            builder.setNegativeButton("Close",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {

                            dialog.cancel();
                        }
                    });

            builder.setView(layout); 
            builder.show();



        }
    });

我有这个代码。当您单击gridView项目时。 alertdialog也将与gridview一起展示。如果用户单击gridView2项目,如何关闭alertdialog?

1 个答案:

答案 0 :(得分:1)

使用AlertDialog

构建AlertDialog Builder
 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);

使用AlertDialog

获取create的实例
Dialog dialog = builder.create();

因此,如果需要,您可以通过致电

解雇
dialog.dismiss();