下面列出了android.view.View#getWindowToken ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void showIndicator(View parent, Rect touchBounds) {
if (isShowing()) {
mPopupView.mMarker.animateOpen();
return;
}
IBinder windowToken = parent.getWindowToken();
if (windowToken != null) {
WindowManager.LayoutParams p = createPopupLayout(windowToken);
p.gravity = Gravity.TOP | GravityCompat.START;
updateLayoutParamsForPosiion(parent, p, touchBounds.bottom);
mShowing = true;
translateViewIntoPosition(touchBounds.centerX());
invokePopup(p);
}
}
public void showIndicator(View parent) {
if (isShowing()) {
// mPopupView.mMarker.animateOpen();
return;
}
IBinder windowToken = parent.getWindowToken();
if (windowToken != null) {
WindowManager.LayoutParams p = createPopupLayout(windowToken);
p.gravity = Gravity.TOP | GravityCompat.START;
updateLayoutParamsForPosiion(parent, p);
mShowing = true;
invokePopup(p);
}
}
public void showIndicator(View parent, Point point) {
if (isShowing()) {
mPopupView.mMarker.animateOpen();
return;
}
IBinder windowToken = parent.getWindowToken();
if (windowToken != null) {
WindowManager.LayoutParams p = createPopupLayout(windowToken);
/** Raw bit controlling whether the layout direction is relative or not (START/END instead of
* absolute LEFT/RIGHT).
*/
int RELATIVE_LAYOUT_DIRECTION = 0x00800000;
/** Push object to x-axis position at the start of its container, not changing its size. */
int START = RELATIVE_LAYOUT_DIRECTION | Gravity.LEFT;
p.gravity = Gravity.TOP | START;
updateLayoutParamsForPosition(parent, p, point.y);
mShowing = true;
translateViewIntoPosition(point.x);
invokePopup(p);
}
}
/**
* Method to hide keyboard if it's open
*/
public void hideKeyboard() {
try {
Activity activity = getActivity();
if (activity == null) {
return;
}
InputMethodManager input
= (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
if (input == null) {
return;
}
View currentFocus = activity.getCurrentFocus();
if (currentFocus == null || currentFocus.getWindowToken() == null) {
return;
}
input.hideSoftInputFromWindow(currentFocus.getWindowToken(), 0);
} catch (Exception e) {
// ignoring any exceptions
}
}
/**
* Bind the dialog with a window token.
* <p>Useful to display it from a service.
*
* @param view The view to bind the dialog.
* @param dialog The dialog to be displayed.
* @param type The dialog type.
* @param windowAnimations The custom animation used for the window.
*
* @return The bound dialog with the supplied view.
*/
public static Dialog bindDialog(@Nullable View view,
@NonNull Dialog dialog, int type, @StyleRes int windowAnimations) {
Window window = dialog.getWindow();
if (window != null) {
if (view != null && view.getWindowToken() != null) {
window.getAttributes().token = view.getWindowToken();
}
window.setType(type);
window.setWindowAnimations(windowAnimations);
window.addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
}
return dialog;
}
/**
* 根据传入控件的坐标和用户的焦点坐标,判断是否隐藏键盘,如果点击的位置在控件内,则不隐藏键盘
*
* @param view
* 控件view
* @param event
* 焦点位置
* @return 是否隐藏
*/
public static void hideKeyboard(MotionEvent event, View view,
Activity activity) {
try {
if (view != null && view instanceof EditText) {
int[] location = { 0, 0 };
view.getLocationInWindow(location);
int left = location[0], top = location[1], right = left
+ view.getWidth(), bootom = top + view.getHeight();
// 判断焦点位置坐标是否在空间内,如果位置在控件外,则隐藏键盘
if (event.getRawX() < left || event.getRawX() > right
|| event.getY() < top || event.getRawY() > bootom) {
// 隐藏键盘
IBinder token = view.getWindowToken();
InputMethodManager inputMethodManager = (InputMethodManager) activity
.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(token,
InputMethodManager.HIDE_NOT_ALWAYS);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void showIndicator(View parent, Rect touchBounds) {
if (isShowing()) {
mPopupView.mMarker.animateOpen();
return;
}
IBinder windowToken = parent.getWindowToken();
if (windowToken != null) {
WindowManager.LayoutParams p = createPopupLayout(windowToken);
p.gravity = Gravity.TOP | GravityCompat.START;
updateLayoutParamsForPosiion(parent, p, touchBounds.bottom);
mShowing = true;
translateViewIntoPosition(touchBounds.centerX());
invokePopup(p);
}
}
private void registerListener(View anchor) {
// Don't do anything if we haven't managed to patch the super listener.
// And don't bother attaching the listener if the anchor view isn't
// attached. This means we'll only have to deal with the real VTO owned
// by the ViewRoot.
if (mSuperScrollListener != null) {
ViewTreeObserver vto = (anchor.getWindowToken() != null) ? anchor.getViewTreeObserver()
: null;
if (vto != mViewTreeObserver) {
if (mViewTreeObserver != null && mViewTreeObserver.isAlive()) {
mViewTreeObserver.removeOnScrollChangedListener(mSuperScrollListener);
}
if ((mViewTreeObserver = vto) != null) {
vto.addOnScrollChangedListener(mSuperScrollListener);
}
}
}
}
private void registerListener(View anchor) {
if (mSuperScrollListener != null) {
ViewTreeObserver vto = (anchor.getWindowToken() != null) ? anchor.getViewTreeObserver() : null;
if (vto != mViewTreeObserver) {
if (mViewTreeObserver != null && mViewTreeObserver.isAlive()) {
mViewTreeObserver.removeOnScrollChangedListener(mSuperScrollListener);
}
if ((mViewTreeObserver = vto) != null) {
vto.addOnScrollChangedListener(mSuperScrollListener);
}
}
}
}
public void run() {
mMenu.changeMenuMode();
final View menuView = (View) mMenuView;
if (menuView != null && menuView.getWindowToken() != null && mPopup.tryShow()) {
mOverflowPopup = mPopup;
}
mPostedOpenRunnable = null;
}
/**
* 隐藏输入法
*
* @param currentFocusView 当前焦点view
*/
public static void hideKeyboard(View currentFocusView) {
if (currentFocusView != null) {
IBinder token = currentFocusView.getWindowToken();
if (token != null) {
InputMethodManager im = (InputMethodManager) currentFocusView.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
im.hideSoftInputFromWindow(token, 0);
}
}
}
@Override
public void onScopeStart(Disposable d) {
disposable = d;
final View view = this.view;
if (view == null)
throw new NullPointerException("view is null");
boolean isAttached = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && view.isAttachedToWindow())
|| view.getWindowToken() != null;
if (!isAttached && !ignoreAttach)
throw new OutsideScopeException("View is not attached!");
view.addOnAttachStateChangeListener(this);
}
public void run() {
mMenu.changeMenuMode();
final View menuView = (View) mMenuView;
if (menuView != null && menuView.getWindowToken() != null && mPopup.tryShow()) {
mOverflowPopup = mPopup;
}
mPostedOpenRunnable = null;
}
protected void removeHeaderViewFromActivity(View headerView) {
mAddHeaderViewRunnable.finish();
if (headerView.getWindowToken() != null) {
mActivity.getWindowManager().removeViewImmediate(headerView);
}
}
public void run() {
mMenu.changeMenuMode();
final View menuView = (View) mMenuView;
if (menuView != null && menuView.getWindowToken() != null && mPopup.tryShow()) {
mOverflowPopup = mPopup;
}
mPostedOpenRunnable = null;
}
/**
* Hides the already popped up keyboard from the screen.
*
* @param context Context to get current focus.
*/
public static void hideKeyboard(Context context) {
try {
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
View currentFocus = ((Activity) context).getCurrentFocus();
if (imm != null && currentFocus != null) {
IBinder windowToken = currentFocus.getWindowToken();
if (windowToken != null) {
imm.hideSoftInputFromWindow(windowToken, 0);
}
}
} catch (Exception e) {
Log.e(LOG_TAG, "Can't even hide keyboard " + e.getMessage());
}
}
private void registerListener(View anchor) {
if (mSuperScrollListener != null) {
ViewTreeObserver vto = (anchor.getWindowToken() != null) ? anchor.getViewTreeObserver() : null;
if (vto != mViewTreeObserver) {
if (mViewTreeObserver != null && mViewTreeObserver.isAlive()) {
mViewTreeObserver.removeOnScrollChangedListener(mSuperScrollListener);
}
if ((mViewTreeObserver = vto) != null) {
vto.addOnScrollChangedListener(mSuperScrollListener);
}
}
}
}
public static void hideSoftKeyboard(Activity act) {
InputMethodManager inputMethodManager = (InputMethodManager) act.getSystemService(Context.INPUT_METHOD_SERVICE);
if(inputMethodManager != null ) {
View localView = act.getCurrentFocus();
if(localView != null && localView.getWindowToken() != null ) {
IBinder windowToken = localView.getWindowToken();
inputMethodManager.hideSoftInputFromWindow(windowToken, 0);
}
}
}
private void handleDestroyActivity(IBinder token, boolean finishing,
int configChanges, boolean getNonConfigInstance) {
ActivityClientRecord r = performDestroyActivity(token, finishing,
configChanges, getNonConfigInstance);
if (r != null) {
cleanUpPendingRemoveWindows(r);
WindowManager wm = r.activity.getWindowManager();
View v = r.activity.mDecor;
if (v != null) {
if (r.activity.mVisibleFromServer) {
mNumVisibleActivities--;
}
IBinder wtoken = v.getWindowToken();
if (r.activity.mWindowAdded) {
if (r.onlyLocalRequest) {
// Hold off on removing this until the new activity's
// window is being added.
r.mPendingRemoveWindow = v;
r.mPendingRemoveWindowManager = wm;
} else {
wm.removeViewImmediate(v);
}
}
if (wtoken != null && r.mPendingRemoveWindow == null) {
WindowManagerImpl.getDefault().closeAll(wtoken,
r.activity.getClass().getName(), "Activity");
}
r.activity.mDecor = null;
}
if (r.mPendingRemoveWindow == null) {
// If we are delaying the removal of the activity window, then
// we can't clean up all windows here. Note that we can't do
// so later either, which means any windows that aren't closed
// by the app will leak. Well we try to warning them a lot
// about leaking windows, because that is a bug, so if they are
// using this recreate facility then they get to live with leaks.
WindowManagerImpl.getDefault().closeAll(token,
r.activity.getClass().getName(), "Activity");
}
// Mocked out contexts won't be participating in the normal
// process lifecycle, but if we're running with a proper
// ApplicationContext we need to have it tear down things
// cleanly.
Context c = r.activity.getBaseContext();
if (c instanceof ContextImpl) {
((ContextImpl) c).scheduleFinalCleanup(
r.activity.getClass().getName(), "Activity");
}
}
if (finishing) {
try {
ActivityManagerNative.getDefault().activityDestroyed(token);
} catch (RemoteException ex) {
// If the system process has died, it's game over for everyone.
}
}
}
static boolean isAttachedToWindow(View view) {
return view.getWindowToken() != null;
}