android开发常用方法
1、 //获取屏幕上每一毫米对应的像素数,此方法有时不太准确,需要手机的标准DPI值 private int getPxOfMM(Context context){ //每1一英寸 == 25.4mm float MMCount = 25.4f; DisplayMetrics dm = context.getResources().getDisplayMetrics(); // //取对角线上的像素数 double DiaonalPixels = Math.sqrt(Math.pow(dm.widthPixels, 2)+ Math.pow(dm.heightPixels, 2)); //取对角线的长度(mm) double diagonalLength = DiaonalPixels / dm.densityDpi * MMCount; //得到每一个毫米所对应的像素数 int result = (int) Math.ceil(DiaonalPixels/diagonalLength); return result; } 2、获取android分辨率 DisplayMetrics dm = new DisplayMetrics(); WindowManager wm = (WindowManager) this.mContext.getSystemService(Context.WINDOW_SERVICE); wm.getDefaultDisplay().getMetrics(dm); 3、在AndroidMainfest.xml中选择哪个activity,设置windowSoftInputMode属性为adjustUnspecified|stateHidden 例如: <activity android:name=".Main" android:label="@string/app_name" android:windowSoftInputMode="adjustUnspecified|stateHidden" android:configChanges="orientation|keyboardHidden"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
4、3、设置AlertDialog 点击按钮不消失
try { Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing"); field.setAccessible(true); field.set(dialog, true); //true 为点击之后消失 ,false 为点击之后不消失 } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); }
5、控制输入法隐藏的代码
((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(getActivity()
.getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);