122 lines
4.7 KiB
Java
122 lines
4.7 KiB
Java
package com.mogo.utils;
|
|
|
|
import android.app.Activity;
|
|
import android.content.Context;
|
|
import android.graphics.Rect;
|
|
import android.os.Build;
|
|
import android.view.MotionEvent;
|
|
import android.view.View;
|
|
import android.view.ViewTreeObserver;
|
|
import android.view.inputmethod.InputMethodManager;
|
|
import android.widget.EditText;
|
|
|
|
public class SoftKeyBoardJobber {
|
|
|
|
public static boolean hideIfNecessary( Activity context, MotionEvent ev ) {
|
|
if ( ev.getAction() == MotionEvent.ACTION_DOWN ) {
|
|
View v = context.getCurrentFocus();
|
|
if ( isShouldHideInput( v, ev ) ) {
|
|
InputMethodManager imm = ( InputMethodManager ) context.getSystemService( Context.INPUT_METHOD_SERVICE );
|
|
if ( imm != null ) {
|
|
imm.hideSoftInputFromWindow( v.getWindowToken(), 0 );
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
private static boolean isShouldHideInput( View v, MotionEvent event ) {
|
|
if ( v != null && ( v instanceof EditText ) ) {
|
|
int[] leftTop = {0, 0};
|
|
// 获取输入框当前的location位置
|
|
v.getLocationInWindow( leftTop );
|
|
int left = leftTop[0];
|
|
int top = leftTop[1];
|
|
int bottom = top + v.getHeight();
|
|
int right = left + v.getWidth();
|
|
return !( event.getX() > left && event.getX() < right && event.getY() > top && event.getY() < bottom );
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public static void hide( Context context, View v ) {
|
|
InputMethodManager imm = getInputMethodManager( context );
|
|
if ( imm != null ) {
|
|
imm.hideSoftInputFromWindow( v.getWindowToken(), 0 );
|
|
}
|
|
}
|
|
|
|
public static void show( Context context ) {
|
|
InputMethodManager imm = getInputMethodManager( context );
|
|
if ( imm != null ) {
|
|
imm.toggleSoftInput( 0, InputMethodManager.HIDE_NOT_ALWAYS );
|
|
}
|
|
}
|
|
|
|
private static InputMethodManager imm = null;
|
|
|
|
private static InputMethodManager getInputMethodManager( Context context ) {
|
|
if ( imm == null ) {
|
|
imm = ( InputMethodManager ) context.getSystemService( Context.INPUT_METHOD_SERVICE );
|
|
}
|
|
return imm;
|
|
}
|
|
|
|
public interface OnSoftKeyboardChangeListener {
|
|
void onSoftKeyBoardChange( int softKeyboardHeight, boolean visible );
|
|
}
|
|
|
|
public static ViewTreeObserver.OnGlobalLayoutListener observeSoftKeyboard( Activity activity, final OnSoftKeyboardChangeListener listener ) {
|
|
if ( !isAliveActivity( activity ) || listener == null ) {
|
|
return null;
|
|
}
|
|
final View decorView = activity.getWindow().getDecorView();
|
|
ViewTreeObserver.OnGlobalLayoutListener onGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
|
|
|
|
int previousKeyboardHeight = -1;
|
|
Rect rect = new Rect();
|
|
boolean lastVisibleState = false;
|
|
|
|
@Override
|
|
public void onGlobalLayout() {
|
|
rect.setEmpty();
|
|
decorView.getWindowVisibleDisplayFrame( rect );
|
|
int displayHeight = rect.bottom - rect.top;
|
|
int height = decorView.getHeight() - rect.top;
|
|
int keyboardHeight = height - displayHeight;
|
|
if ( previousKeyboardHeight != keyboardHeight ) {
|
|
boolean hide = ( double ) displayHeight / height > 0.8;
|
|
if ( hide != lastVisibleState ) {
|
|
listener.onSoftKeyBoardChange( keyboardHeight, !hide );
|
|
lastVisibleState = hide;
|
|
}
|
|
}
|
|
previousKeyboardHeight = height;
|
|
}
|
|
};
|
|
decorView.getViewTreeObserver().addOnGlobalLayoutListener( onGlobalLayoutListener );
|
|
return onGlobalLayoutListener;
|
|
}
|
|
|
|
public static void removeSoftKeyboardObserver( Activity activity, ViewTreeObserver.OnGlobalLayoutListener listener ) {
|
|
if ( !isAliveActivity( activity ) || listener == null ) {
|
|
return;
|
|
}
|
|
final View decorView = activity.getWindow().getDecorView();
|
|
if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN ) {
|
|
decorView.getViewTreeObserver().removeOnGlobalLayoutListener( listener );
|
|
} else {
|
|
decorView.getViewTreeObserver().removeGlobalOnLayoutListener( listener );
|
|
}
|
|
}
|
|
|
|
private static boolean isAliveActivity( Activity activity ) {
|
|
return activity != null
|
|
&& activity.getWindow() != null
|
|
&& activity.getWindow().getDecorView() != null
|
|
&& activity.getWindow().getDecorView().getViewTreeObserver() != null;
|
|
}
|
|
|
|
}
|