1.获取手机状态栏的高度
/* * Copyright (C) 2015-2016 Jacksgong(blog.dreamtobe.cn) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.example.pengganggui.choiceCity.utils; import android.content.Context; import android.util.Log; /** * @author luck update 2015/4/21 16:00 Created by on 3/26/16. Jacksgong * <p/> * In order to avoid the layout of the Status bar. */ public class StatusBarHeightUtil { private final static String STATUS_BAR_DEF_PACKAGE = "android"; private final static String STATUS_BAR_DEF_TYPE = "dimen"; private final static String STATUS_BAR_NAME = "status_bar_height"; private static boolean INIT = false; private static int STATUS_BAR_HEIGHT = 50; public static synchronized int getStatusBarHeight(final Context context) { if (!INIT) { int resourceId = context.getResources(). getIdentifier(STATUS_BAR_NAME, STATUS_BAR_DEF_TYPE, STATUS_BAR_DEF_PACKAGE); if (resourceId > 0) { STATUS_BAR_HEIGHT = context.getResources().getDimensionPixelSize(resourceId); INIT = true; Log.d("StatusBarHeightUtil", String.format("Get status bar height %d", STATUS_BAR_HEIGHT)); } } return STATUS_BAR_HEIGHT; } }
2.键盘处理类
package com.example.pengganggui.choiceCity.utils;/* * Copyright (C) 2015-2016 Jacksgong(blog.dreamtobe.cn) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import android.app.Activity; import android.content.Context; import android.graphics.Rect; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.view.ViewTreeObserver; import android.view.WindowManager; import android.view.inputmethod.InputMethodManager; import android.widget.EditText; /** * @author luck update 2015/4/21 16:00 Created by Jacksgong on 15/7/6. * <p/> * For save the keyboard height, and provide the valid-panel-height {}. * <p/> * Adapt the panel height with the keyboard height just relate {@link #attach(Activity, IPanelHeightTarget)}. */ public class KeyboardUtil { /** * 显示键盘 */ public static void showKeyboard(final View view) { view.requestFocus(); InputMethodManager inputManager = (InputMethodManager) view.getContext().getSystemService( Context.INPUT_METHOD_SERVICE); inputManager.showSoftInput(view, 0); } /** * 打开软键盘 * * @param mEditText 输入框 * @param mContext 上下文 */ public static void openKeybord(EditText mEditText, Context mContext) { InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(mEditText, InputMethodManager.RESULT_SHOWN); imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY); } /** * 关闭隐藏键盘 */ public static void hideKeyboard(final View view) { InputMethodManager imm = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(view.getWindowToken(), 0); } /** * Recommend invoked by {@link Activity#onCreate(Bundle)} * For align the height of the keyboard to {@code target} as much as possible. * For save the refresh the keyboard height to shared-preferences. * * @param activity contain the view * @param target whose height will be align to the keyboard height. */ public static void attach(final Activity activity, IPanelHeightTarget target) { final ViewGroup contentView = activity.findViewById(android.R.id.content); boolean fullScreen = ((activity.getWindow().getAttributes().flags & WindowManager.LayoutParams.FLAG_FULLSCREEN) != 0); contentView.getViewTreeObserver(). addOnGlobalLayoutListener(new KeyboardStatusListener(fullScreen, contentView, target)); } private static class KeyboardStatusListener implements ViewTreeObserver.OnGlobalLayoutListener { private final ViewGroup contentView; private final IPanelHeightTarget panelHeightTarget; private final boolean isFullScreen; private final int statusBarHeight; private int previousDisplayHeight = 0; private boolean lastKeyboardShowing; private int maxOverlayLayoutHeight; KeyboardStatusListener(boolean isFullScreen, ViewGroup contentView, IPanelHeightTarget panelHeightTarget) { this.contentView = contentView; this.panelHeightTarget = panelHeightTarget; this.isFullScreen = isFullScreen; this.statusBarHeight = StatusBarHeightUtil.getStatusBarHeight(contentView.getContext()); } @Override public void onGlobalLayout() { final View userRootView = contentView.getChildAt(0); Rect r = new Rect(); userRootView.getWindowVisibleDisplayFrame(r); final int displayHeight = (r.bottom - r.top); calculateKeyboardHeight(displayHeight); calculateKeyboardShowing(displayHeight); previousDisplayHeight = displayHeight; } private void calculateKeyboardHeight(final int displayHeight) { if (previousDisplayHeight == 0) { previousDisplayHeight = displayHeight; return; } } private void calculateKeyboardShowing(final int displayHeight) { boolean isKeyboardShowing; final View actionBarOverlayLayout = (View) contentView.getParent(); final int actionBarOverlayLayoutHeight = actionBarOverlayLayout.getHeight() - actionBarOverlayLayout.getPaddingTop(); if (isFullScreen) { if (actionBarOverlayLayoutHeight - displayHeight == this.statusBarHeight) { isKeyboardShowing = lastKeyboardShowing; } else { isKeyboardShowing = actionBarOverlayLayoutHeight > displayHeight; } } else { final int phoneDisplayHeight = contentView.getResources().getDisplayMetrics().heightPixels; if (phoneDisplayHeight == actionBarOverlayLayoutHeight) { return; } if (maxOverlayLayoutHeight == 0) { isKeyboardShowing = lastKeyboardShowing; } else isKeyboardShowing = displayHeight < maxOverlayLayoutHeight; maxOverlayLayoutHeight = Math.max(maxOverlayLayoutHeight, actionBarOverlayLayoutHeight); } if (lastKeyboardShowing != isKeyboardShowing) { this.panelHeightTarget.onKeyboardShowing(isKeyboardShowing); } lastKeyboardShowing = isKeyboardShowing; } } }
3.判断是否显示软键盘接口
/* * Copyright (C) 2015-2016 Jacksgong(blog.dreamtobe.cn) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.example.pengganggui.choiceCity.utils; /** * @author luck update 2015/4/21 16:00 Created by Jacksgong on 3/26/16. * <p/> * For align the height of the keyboard to panel height as much as possible. */ public interface IPanelHeightTarget { /** * Be invoked by onGlobalLayoutListener call-back. * * @param showing whether the keyboard is showing or not. */ void onKeyboardShowing(boolean showing); }