79 lines
2.4 KiB
Plaintext
Executable File
79 lines
2.4 KiB
Plaintext
Executable File
package com.example.administrator.ximengjianyu.utils;
|
|
|
|
import android.content.Context;
|
|
import android.util.DisplayMetrics;
|
|
import android.view.Display;
|
|
import android.view.WindowManager;
|
|
import android.view.inputmethod.InputMethodManager;
|
|
|
|
import java.lang.reflect.Method;
|
|
|
|
/**
|
|
* Created by xukai on 2017/2/13.
|
|
* 关于输入的一些工具类
|
|
*/
|
|
public class InputUtils {
|
|
|
|
/**
|
|
* 该方法隐藏软键盘
|
|
*/
|
|
public void guideSoftInput(Context context) {
|
|
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
|
|
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
|
|
}
|
|
|
|
/**
|
|
* 该方法设置软键盘可见
|
|
*/
|
|
public void showSoftInput(Context context) {
|
|
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
|
|
imm.toggleSoftInput(0, InputMethodManager.SHOW_FORCED);
|
|
}
|
|
|
|
public int getDpi(Context context) {
|
|
int dpi = 0;
|
|
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
|
|
Display display = windowManager.getDefaultDisplay();
|
|
DisplayMetrics displayMetrics = new DisplayMetrics();
|
|
@SuppressWarnings("rawtypes")
|
|
Class c;
|
|
try {
|
|
c = Class.forName("android.view.Display");
|
|
@SuppressWarnings("unchecked")
|
|
Method method = c.getMethod("getRealMetrics", DisplayMetrics.class);
|
|
method.invoke(display, displayMetrics);
|
|
dpi = displayMetrics.heightPixels;
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
return dpi;
|
|
}
|
|
|
|
/**
|
|
* 获取 虚拟按键的高度
|
|
* @param context
|
|
* @return
|
|
*/
|
|
public int getBottomStatusHeight(Context context) {
|
|
int totalHeight = getDpi(context);
|
|
|
|
int contentHeight = getScreenHeight(context);
|
|
|
|
return totalHeight - contentHeight;
|
|
}
|
|
|
|
/**
|
|
* 获得屏幕高度
|
|
*
|
|
* @param context
|
|
* @return
|
|
*/
|
|
public int getScreenHeight(Context context) {
|
|
WindowManager wm = (WindowManager) context
|
|
.getSystemService(Context.WINDOW_SERVICE);
|
|
DisplayMetrics outMetrics = new DisplayMetrics();
|
|
wm.getDefaultDisplay().getMetrics(outMetrics);
|
|
return outMetrics.heightPixels;
|
|
}
|
|
}
|