Skip to content

Commit

Permalink
add inputview
Browse files Browse the repository at this point in the history
  • Loading branch information
wufanghan committed Mar 5, 2021
1 parent f49ffe1 commit fb2456b
Show file tree
Hide file tree
Showing 8 changed files with 659 additions and 4 deletions.
4 changes: 3 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ dependencies {
implementation 'androidx.multidex:multidex:2.0.1'

//permissions
implementation 'com.github.tbruyelle:rxpermissions:0.11'
implementation 'com.github.tbruyelle:rxpermissions:0.12'

//rx
implementation 'io.reactivex.rxjava3:rxjava:3.0.4'
Expand Down Expand Up @@ -93,4 +93,6 @@ dependencies {
//滴滴的调试插件 https://github.com/didi/DoraemonKit
debugImplementation 'com.didichuxing.doraemonkit:doraemonkit:3.1.4'
releaseImplementation 'com.didichuxing.doraemonkit:doraemonkit-no-op:2.0.2'

api 'com.github.LuckSiege.PictureSelector:picture_library:v2.5.9'
}
143 changes: 143 additions & 0 deletions app/src/main/java/com/example/baseframe/utils/SoftInputUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package com.example.baseframe.utils;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Rect;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

/**
* private void attachView() {
*
*
* //editText2为需要调整的View
* editText2 = findViewById(R.id.et2);
* SoftInputUtil softInputUtil = new SoftInputUtil();
* softInputUtil.attachSoftInput(editText2, new SoftInputUtil.ISoftInputChanged() {
* @Override
* public void onChanged(boolean isSoftInputShow, int softInputHeight, int viewOffset) {
* if (isSoftInputShow) {
* editText2.setTranslationY(et2.getTranslationY() - viewOffset);
* } else {
* editText2.setTranslationY(0);
* }
* }
* });
* }
*
* android:windowSoftInputMode="adjustResize|stateAlwaysHidden"
*/
public class SoftInputUtils {
private int softInputHeight = 0;
private boolean softInputHeightChanged = false;

private boolean isNavigationBarShow = false;
private int navigationHeight = 0;

private View anyView;
private ISoftInputChanged listener;
private boolean isSoftInputShowing = false;

public interface ISoftInputChanged {
void onChanged(boolean isSoftInputShow, int softInputHeight, int viewOffset);
}

public void attachSoftInput(final View anyView, final ISoftInputChanged listener) {
if (anyView == null || listener == null)
return;

//根View
final View rootView = anyView.getRootView();
if (rootView == null)
return;

navigationHeight = getNavigationBarHeight(anyView.getContext());

//anyView为需要调整高度的View,理论上来说可以是任意的View
this.anyView = anyView;
this.listener = listener;

rootView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
//对于Activity来说,该高度即为屏幕高度
int rootHeight = rootView.getHeight();
Rect rect = new Rect();
//获取当前可见部分,默认可见部分是除了状态栏和导航栏剩下的部分
rootView.getWindowVisibleDisplayFrame(rect);

if (rootHeight - rect.bottom == navigationHeight) {
//如果可见部分底部与屏幕底部刚好相差导航栏的高度,则认为有导航栏
isNavigationBarShow = true;
} else if (rootHeight - rect.bottom == 0) {
//如果可见部分底部与屏幕底部平齐,说明没有导航栏
isNavigationBarShow = false;
}

//cal softInput height
boolean isSoftInputShow = false;
int softInputHeight = 0;
//如果有导航栏,则要去除导航栏的高度
int mutableHeight = isNavigationBarShow == true ? navigationHeight : 0;
if (rootHeight - mutableHeight > rect.bottom) {
//除去导航栏高度后,可见区域仍然小于屏幕高度,则说明键盘弹起了
isSoftInputShow = true;
//键盘高度
softInputHeight = rootHeight - mutableHeight - rect.bottom;
if (SoftInputUtils.this.softInputHeight != softInputHeight) {
softInputHeightChanged = true;
SoftInputUtils.this.softInputHeight = softInputHeight;
} else {
softInputHeightChanged = false;
}
}

//获取目标View的位置坐标
int[] location = new int[2];
anyView.getLocationOnScreen(location);

//条件1减少不必要的回调,只关心前后发生变化的
//条件2针对软键盘切换手写、拼音键等键盘高度发生变化
if (isSoftInputShowing != isSoftInputShow || (isSoftInputShow && softInputHeightChanged)) {
if (listener != null) {
//第三个参数为该View需要调整的偏移量
//此处的坐标都是相对屏幕左上角(0,0)为基准的
listener.onChanged(isSoftInputShow, softInputHeight, location[1] + anyView.getHeight() - rect.bottom);
}

isSoftInputShowing = isSoftInputShow;
}
}
});
}


//***************STATIC METHOD******************

public static int getNavigationBarHeight(Context context) {
if (context == null)
return 0;
Resources resources = context.getResources();
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
int height = resources.getDimensionPixelSize(resourceId);
return height;
}

public static void showSoftInput(View view) {
if (view == null)
return;
InputMethodManager inputMethodManager = (InputMethodManager)view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputMethodManager != null) {
inputMethodManager.showSoftInput(view, 0);
}
}

public static void hideSoftInput(View view) {
if (view == null)
return;
InputMethodManager inputMethodManager = (InputMethodManager)view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputMethodManager != null) {
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
}
Loading

0 comments on commit fb2456b

Please sign in to comment.