2 jonskeet JonSkeet 于 2013.08.16 14:44 提问

当我点击ListView item时如何关闭AlertDialog?

下面是 dialog 代码,在 dialog 中有一个 ListView,我想在点击 ListView item 时关闭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();     
    }

我想在onItemClick()方法中加入代码

但是不能执行。
怎么处理?

2个回答

longhai326
longhai326   2013.08.19 15:21
已采纳
private void windowDialog(){
      Window mWindow = newDialogView();
      mWindow.setContentView(R.layout.kl_dialog_invite_code);
      ListView listview=mWindow.findViewById(R.id.xxx);
      //在onItemClick事件中调用mDialog.dismiss();就ok了
}

private Window newDialogView() {
        if (mDialog == null) {
            mDialog = new Dialog(this, R.style.mydialog);
            mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        }
        mDialog.show();
        mDialog.setCanceledOnTouchOutside(true);
        mDialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
        Window win = mDialog.getWindow();
        WindowManager.LayoutParams lp = win.getAttributes();
        // lp.alpha = 0.9f;
        win.setAttributes(lp);
        // WindowManager.LayoutParams.FLAG_DIM_BEHIND 背景渐变
        // WindowManager.LayoutParams.FLAG_BLUR_BEHIND 背景模糊
        win.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        return win;
}
Jave_
Jave_   2013.08.17 20:59
searchBuilder.dismiss().

是这个关闭吗

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!