下面是 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()
方法中加入代碼
但是不能執行。
怎麼處理?
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;
}