android当activity加载完成后自动弹出popWindow

通过重写方法onWindowFocusChanged(boolean hasFocus):

@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		super.onWindowFocusChanged(hasFocus);
		if (!flag) {
			if (accountList.size() != 0) {
				popWindow = new EcardPopupMenu(ECardActivity.this, accountList,
						getResources().getColor(R.color.blue), 0);
				Display display = this.getWindowManager().getDefaultDisplay();
				Point size = new Point();
				display.getSize(size);
				int width = size.x;
				popWindow.setWidth((int) ((width) * 0.5));
				// 必须要在设置显示位置之前设置背景
				ColorDrawable dw = new ColorDrawable(getResources().getColor(
						R.color.background_disenable));
				popWindow.setBackgroundDrawable(dw);
				popWindow.showLocation(R.id.ecd_linearlayout01);
				popWindow.setOnItemClickListener(new OnItemClickListener() {
					@Override
					public void onClick(String str) {
						// TODO Auto-generated method stub
						currentAcount = str;
						mTvCurrentAccount.setText(str);
						List datas = getEcardTitleContentData(str, map);
						mAdapter.notifyDataSetChanged(datas);
						popWindow.dismiss();
					}
				});
				flag = true;
			}
		}

	}

这里的flag是用来标识activity第一次加载的,当activity是第一次加载则自动弹出一个popwindow;如果不设置这一个标识,则会出现重复弹出popwindow的情况,
因为每当一个新的View加载完成时都会调用onWindowFocusChanged(boolean hasFocus),所以每当一个新的popWindow加载完成时又会重新调用onWindowFocusChanged(boolean hasFocus),

你可能感兴趣的:(android笔记)