ToolWindow tool class, popupwindow tool class
Package com. dute. dutenews. utils; import android. app. activity; import android. content. context; import android. view. view; import android. view. windowManager; import android. view. inputmethod. inputMethodManager; public class ToolWindow {/*** sets the background transparency of the screen to be added. ***/public static void setBackgroundAlpha (Activity context, float bgAlpha) {WindowManager. layoutParams lp = context. getWindow (). getAttributes (); lp. alpha = bgAlpha; // 0.0-1.0 context. getWindow (). setAttributes (lp);}/** hide keypad */public static void hideSoftInputFromWindow (Context context, View view) {// 1. obtain the InputMethodManager object InputMethodManager imm = (InputMethodManager) context. getSystemService (Context. INPUT_METHOD_SERVICE); // 2. call hideSoftInputFromWindow to hide the keyboard imm. hideSoftInputFromWindow (view. getWindowToken (), 0); // force hide the keyboard}/*** display the soft keyboard ** @ param context * @ param view */public static void showSoftInput (Context context, view view) {// 1. obtain the InputMethodManager object InputMethodManager imm = (InputMethodManager) context. getSystemService (Context. INPUT_METHOD_SERVICE); // 2. call the showSoftInput method to display the keyboard, where view is the focused view component imm. showSoftInput (view, InputMethodManager. SHOW_FORCED);}/*** implement the function of switching to display the soft keyboard ** @ param context * @ param view */public static void toggleSoftInput (Context context) {// 1. obtain the InputMethodManager object InputMethodManager imm = (InputMethodManager) context. getSystemService (Context. INPUT_METHOD_SERVICE); // 2. implement the function of switching to display the keyboard imm. toggleSoftInput (0, InputMethodManager. HIDE_NOT_ALWAYS);}/*** if true is returned, the input method is enabled. **/public static boolean isActive (Context context) {// 1. obtain the InputMethodManager object InputMethodManager imm = (InputMethodManager) context. getSystemService (Context. INPUT_METHOD_SERVICE); return imm. isActive ();}/*** convert the unit from dp to px (pixel) */public static int dip2px (Context context, float dpValue) based on the cell phone resolution) {final float scale = context. getResources (). getDisplayMetrics (). density; return (int) (dpValue * scale) + 0.5f);}/*** based on the resolution of the phone) to dp */public static int px2dip (Context context, float pxValue) {final float scale = context. getResources (). getDisplayMetrics (). density; return (int) (pxValue/scale) + 0.5f );}}