1. 程式人生 > >多個Popupwindow同時彈出時實現分別拖拽功能

多個Popupwindow同時彈出時實現分別拖拽功能

Popupwindow一般預設點選彈窗外區域,視窗即消失,

這與一條語句有關:popupWindow.setBackgroundDrawable(new BitmapDrawable())

將此語句刪掉即可取消這個功能

然而要實現多個Popupwindow可分別拖拽,還要在建popupwindow時將Focusable設為false

具體程式碼見:

	private void showpopwindow() {
		// TODO Auto-generated method stub
		final PopupWindow remindwindow;
		View view ;
		LayoutInflater mLayoutInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		view = mLayoutInflater.inflate(R.layout.remindinfopopwindowup, null);

		remindwindow = new PopupWindow(view, 300, 275);
		TextView remindtext = (TextView) view
				.findViewById(R.id.remindinfotextview);
		remindtext.setText(remindinfo.getRemindmessage());
		remindwindow.setAnimationStyle(android.R.style.Animation);
		remindwindow.setOutsideTouchable(true);
		remindwindow.update();
		remindwindow.setTouchable(true);
		remindwindow.setFocusable(false);
                //拖拽功能實現
		view.setOnTouchListener((new OnTouchListener() {
			int mScreenX = 0;
			int mScreenY = 0;
			int mX, mY;

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					v.setFocusable(true);
					mX = (int) event.getX();
					mY = (int) event.getY();
					break;
				case MotionEvent.ACTION_MOVE:
					int delX = (int) (event.getX() - mX);
					int delY = (int) (event.getY() - mY);
					mScreenX += delX;
					mScreenY += delY;
					remindwindow.update(mScreenX, mScreenY, -1, -1);
					break;
				case MotionEvent.ACTION_UP:
					break;
				}
				return true;
			}
		}));

		if (!remindwindow.isShowing()) {
			if (v != null) {
  			    remindwindow.showAsDropDown(v, -160, -15);
			} else {
  			    remindwindow.showAtLocation(view, Gravity.LEFT, 0, -100);
			}
		}
		CountDownTimer timer = new CountDownTimer(10000, 1000) {
			@Override
			public void onTick(long millisUntilFinished) {
			}

			@Override
			public void onFinish() {
				if (remindwindow.isShowing())
					remindwindow.dismiss();
			}
		};
		timer.start();
	}
最後的countDownTimer是popupwindow的限時顯示功能實現

這樣在彈出多個此種popupwindow後,就可以分別實現拖拽操作了