美文网首页
Android开发常用集锦(持续更新...)

Android开发常用集锦(持续更新...)

作者: Shawpoo的 | 来源:发表于2018-02-28 10:52 被阅读11次

    转载请注明出处:https://www.jianshu.com/p/691278bea1f9
    本文出自Shawpoo的简书
    我的博客:CSDN博客

    1、TextView添加删除线

       /**
         * TextView添加删除线
         *
         * @param textView
         */
        public static void setDeleteLine(TextView textView) {
            textView.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        }
    

    2、Java代码中给TextView设置Drawable

      /**
         * TextView设置drawable,不设置传0
         *
         * @param context
         * @param leftResId   左
         * @param topResId    上
         * @param rightResId  右
         * @param bottomResId 下
         * @param textView
         */
        public static void setTextDrawable(Context context, int leftResId, int topResId, int rightResId, int bottomResId, TextView textView) {
            Drawable drawableLeft = leftResId == 0 ? null : context.getResources().getDrawable(leftResId);
            Drawable drawableTop = topResId == 0 ? null : context.getResources().getDrawable(topResId);
            Drawable drawableRight = rightResId == 0 ? null : context.getResources().getDrawable(rightResId);
            Drawable drawableBottom = bottomResId == 0 ? null : context.getResources().getDrawable(bottomResId);
            textView.setCompoundDrawablesWithIntrinsicBounds(drawableLeft, drawableTop, drawableRight, drawableBottom);
        }
    

    3、TabLayout设置横线的左右间隔

     /**
         * 设置TabLayout tab横线的长度
         *
         * @param tabs
         * @param leftDip  左边的间隔
         * @param rightDip 右边的间隔
         */
        public static void setTabLayoutIndicator(TabLayout tabs, int leftDip, int rightDip) {
            Class<?> tabLayout = tabs.getClass();
            Field tabStrip = null;
            try {
                tabStrip = tabLayout.getDeclaredField("mTabStrip");
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
    
            tabStrip.setAccessible(true);
            LinearLayout llTab = null;
            try {
                llTab = (LinearLayout) tabStrip.get(tabs);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
    
            int left = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, leftDip, Resources.getSystem().getDisplayMetrics());
            int right = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, rightDip, Resources.getSystem().getDisplayMetrics());
    
            for (int i = 0; i < llTab.getChildCount(); i++) {
                View child = llTab.getChildAt(i);
                child.setPadding(0, 0, 0, 0);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
                params.leftMargin = left;
                params.rightMargin = right;
                child.setLayoutParams(params);
                child.invalidate();
            }
        }
    

    4、调用拨号界面

      /**
        * 调用拨号界面
        *
        * @param phone 电话号码
        */
        public static void call(Context context, String phone) {
            Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        }
    

    5、调用手机浏览器

       /**
         * 调用手机浏览器
         *
         * @param url
         */
        public static void openBrowser(Context context, String url) {
            Intent intent = new Intent();
            intent.setAction("android.intent.action.VIEW");
            Uri content_url = Uri.parse(url);
            intent.setData(content_url);
            context.startActivity(intent);
        }
    

    6、隐藏软键盘

       /**
         * 隐藏软键盘
         *
         * @param context
         */
        public static void systemSoftInput(Context context, View view) {
            InputMethodManager imm = (InputMethodManager) context.getSystemService(Context
                    .INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    

    7、判断应用是否安装

       /**
         * 判断应用是否安装
         *
         * @param packageName
         * @return
         */
        public static boolean checkApkExist(Context context, String packageName) {
            if (packageName == null || "".equals(packageName))
                return false;
            try {
                ApplicationInfo info = context.getPackageManager().getApplicationInfo(packageName,
                        PackageManager.GET_UNINSTALLED_PACKAGES);
                return true;
            } catch (PackageManager.NameNotFoundException e) {
                return false;
            }
        }
    

    8、获取APP版本号或版本名称

    /**
         * 获取APP版本号
         */
        public static int getAppVersionCode(Context context) {
            int localVersion = 0;
            try {
                PackageInfo packageInfo = context.getApplicationContext()
                        .getPackageManager()
                        .getPackageInfo(context.getPackageName(), 0);
                localVersion = packageInfo.versionCode;
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
            return localVersion;
        }
    
        /**
         * 获取APP版本名称
         */
        public static String getAppVersionName(Context context) {
            String localVersion = "";
            try {
                PackageInfo packageInfo = context.getApplicationContext()
                        .getPackageManager()
                        .getPackageInfo(context.getPackageName(), 0);
                localVersion = packageInfo.versionName;
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
            return localVersion;
        }
    

    9、strings.xml 中的使用技巧

    • 空格:两个&#160;占半个字符,一个\u3000占一个字符。
    • 模板:%1$s,%1$d可以用来当做模板。

    持续更新...

    相关文章

      网友评论

          本文标题:Android开发常用集锦(持续更新...)

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