美文网首页
安卓Util

安卓Util

作者: 呵鲲 | 来源:发表于2017-09-22 23:04 被阅读0次

    SharedPreferenceUtil

    public class SharedPreferenceUtil {
    
        private static SharedPreferences getAppSp() {
            return App.getInstance().getSharedPreferences(SHAREDPREFERENCES_NAME, Context.MODE_PRIVATE);
        }
        
        //其他类型类推
        public static boolean getFirstLoad() {
            return getAppSp().getBoolean(FIRST_LOAD, DEFAULT_FIRST_LOAD);
        }
    
        public static void setFirstLoad(boolean state) {
            getAppSp().edit().putBoolean(FIRST_LOAD, state).apply();
        }
    }
    

    BottomDialogUtil底部弹窗

    public class BottomDialogUtil {
        private static Dialog bottomDialog;
    
        public static View showBottomDialog(Context context, int layout) {
            bottomDialog = new Dialog(context, R.style.BottomDialog);
            View contentView = LayoutInflater.from(context).inflate(layout, null);
            bottomDialog.setContentView(contentView);
            ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) contentView.getLayoutParams();
            layoutParams.width = DeviceUtil.getScreenWidth(context) - DeviceUtil.dp2px(context, 16f);
            layoutParams.bottomMargin = DeviceUtil.dp2px(context, 8f);
            contentView.setLayoutParams(layoutParams);
            bottomDialog.getWindow().setGravity(Gravity.BOTTOM);
            bottomDialog.getWindow().setWindowAnimations(R.style.BottomDialog_Animation);
            bottomDialog.setCanceledOnTouchOutside(true);
            bottomDialog.show();
            return contentView;
        }
    
        public static void dismissBottomDialog() {
            bottomDialog.dismiss();
        }
    }
    
    <style name="BottomDialog" parent="@style/Base.V7.Theme.AppCompat.Light.Dialog">
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowBackground">@android:color/transparent</item>
    </style>
    
    <style name="BottomDialog.Animation" parent="Animation.AppCompat.Dialog">
        <item name="android:windowEnterAnimation">@anim/translate_dialog_in</item>
        <item name="android:windowExitAnimation">@anim/translate_dialog_out</item>
    </style>
    
    <translate xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="100"
        android:fromXDelta="0"
        android:fromYDelta="100%"
        android:toXDelta="0"
        android:toYDelta="0">
    </translate>
    
    <translate xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="100"
        android:fromXDelta="0"
        android:fromYDelta="0"
        android:toXDelta="0"
        android:toYDelta="100%">
    </translate>
    

    软键盘切换

    public class KeyBoardUtil {
        public static void showKeyboard(Context mContext, EditText mEditText) {
            InputMethodManager imm = (InputMethodManager) mContext
                    .getSystemService(Context.INPUT_METHOD_SERVICE);
            if (imm != null) {
                imm.showSoftInput(mEditText, InputMethodManager.SHOW_FORCED);
            }
        }
    
        public static void hideKeyboard(Context mContext, EditText mEditText) {
            InputMethodManager imm = (InputMethodManager) mContext
                    .getSystemService(Context.INPUT_METHOD_SERVICE);
            if (imm != null) {
                imm.hideSoftInputFromWindow(mEditText.getWindowToken(),
                        InputMethodManager.HIDE_NOT_ALWAYS);
            }
    
        }
    
        public static void toggleKeyboard(Context mContext) {
            InputMethodManager imm = (InputMethodManager) mContext
                    .getSystemService(Context.INPUT_METHOD_SERVICE);
            if (imm != null) {
                imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT,
                        InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:安卓Util

          本文链接:https://www.haomeiwen.com/subject/isdnrxtx.html