en經(jīng)過幾個(gè)項(xiàng)目的開發(fā)烘豹,對(duì)于Android 軟鍵盤的一些常見問題和解決方案做一下總結(jié)竖配。
鍵盤的顯示與隱藏
先附上一個(gè)寫好的工具類:
/**
* Created by YBJ on 2016/1/14.
* 顯示鍵盤d工具類
*/
public class InputMethodUtils {
/** 顯示軟鍵盤 */
public static void showInputMethod(View view) {
InputMethodManager imm = (InputMethodManager) view.getContext()
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
}
}
/** 隱藏軟鍵盤 */
public static void showInputMethod(Context context) {
InputMethodManager imm = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
}
/** 多少時(shí)間后顯示軟鍵盤 */
public static void showInputMethod(final View view, long delayMillis) {
if (view==null)return;
// 顯示輸入法
view.postDelayed(new Runnable() {
@Override
public void run() {
InputMethodUtils.showInputMethod(view);
}
}, delayMillis);
}
}
解決底部輸入框與鍵盤覆蓋沖突
/**
* 解決底部輸入框和軟鍵盤的問題
* Created by geyifeng on 2017/5/17.
*/
public class KeyboardPatch {
private Activity mActivity;
private View mDecorView;
private View mContentView;
private boolean flag = false;
private KeyboardPatch(Activity activity) {
this(activity, activity.findViewById(android.R.id.content));
}
private KeyboardPatch(Activity activity, View contentView) {
this.mActivity = activity;
this.mDecorView = activity.getWindow().getDecorView();
this.mContentView = contentView;
if (contentView.equals(activity.findViewById(android.R.id.content))) {
this.flag = false;
} else {
this.flag = true;
}
}
/**
* Patch keyboard patch.
*
* @param activity the activity
* @return the keyboard patch
*/
public static KeyboardPatch patch(Activity activity) {
return new KeyboardPatch(activity);
}
/**
* Patch keyboard patch.
*
* @param activity the activity
* @param contentView 界面容器虾标,指定布局的根節(jié)點(diǎn)
* @return the keyboard patch
*/
public static KeyboardPatch patch(Activity activity, View contentView) {
return new KeyboardPatch(activity, contentView);
}
/**
* 監(jiān)聽layout變化
*/
public void enable() {
// mActivity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN
// | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
mActivity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
mDecorView.getViewTreeObserver().addOnGlobalLayoutListener(onGlobalLayoutListener);//當(dāng)在一個(gè)視圖樹中全局布局發(fā)生改變或者視圖樹中的某個(gè)視圖的可視狀態(tài)發(fā)生改變時(shí)另绩,所要調(diào)用的回調(diào)函數(shù)的接口類
}
}
/**
* 取消監(jiān)聽
*/
public void disable() {
// mActivity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN
// | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
mActivity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
mDecorView.getViewTreeObserver().removeOnGlobalLayoutListener(onGlobalLayoutListener);
}
}
private ViewTreeObserver.OnGlobalLayoutListener onGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect r = new Rect();
mDecorView.getWindowVisibleDisplayFrame(r); //獲取當(dāng)前窗口可視區(qū)域大小的
int height = mDecorView.getContext().getResources().getDisplayMetrics().heightPixels; //獲取屏幕密度爆班,不包含導(dǎo)航欄
int diff = height - r.bottom;
if (diff > 0) {
if (mContentView.getPaddingBottom() != diff) {
if (flag || (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && !OSUtils.isEMUI3_1())) {
mContentView.setPadding(0, 0, 0, diff);
} else {
mContentView.setPadding(0, 0, 0, diff + TranslucentBar.getNavigationBarHeight(mActivity));
}
}
} else {
if (mContentView.getPaddingBottom() != 0) {
if (flag || (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && !OSUtils.isEMUI3_1())) {
mContentView.setPadding(0, 0, 0, 0);
} else {
mContentView.setPadding(0, 0, 0, TranslucentBar.getNavigationBarHeight(mActivity));
}
}
}
}
};
}