Android常用代码片段(笔记一)


 1.自定义广播 接受网络状态

public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        NetworkInfo wifiNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (!mobNetInfo.isConnected() && !wifiNetInfo.isConnected()) {
            Toast.makeText(context, "检测到当前并未连接网络......",Toast.LENGTH_SHORT).show();
        } else if (wifiNetInfo.isConnected()) {
            Toast.makeText(context, "正在使用wifi连接,请放心使用......", Toast.LENGTH_SHORT).show();
        } else if (mobNetInfo.isConnected()) {
            Toast.makeText(context, "检测到正在使用移动网络,可能会消耗很多流量,建议在wifi下浏览.....", Toast.LENGTH_SHORT).show();
        } 
    }
}



2.字体类型和大小
public class FontsType {
    //字体的
    static String 行楷 = "fonts/xingkai.ttf";
    static String 宋楷 = "fonts/songti.ttf";
    static String 鱼楷 = "fonts/yuti.ttf";
    static String 楷体 = "fonts/kaiti.ttf";

    public static Typeface type(AssetManager assets, String path) {
        if (path.equals(行楷)) {
            Typeface typeface1 = Typeface.createFromAsset(assets, 行楷);
            return typeface1;
        }
        if (path.equals(宋楷)) {
            Typeface typeface1 = Typeface.createFromAsset(assets, 宋楷);
            return typeface1;
        }
        if (path.equals(鱼楷)) {
            Typeface typeface1 = Typeface.createFromAsset(assets, 鱼楷);
            return typeface1;
        }else {
            Typeface typeface1 = Typeface.createFromAsset(assets, 楷体);
            return typeface1;
        }
    }
    //字体的大小
    public static float fontScale(Context context, float scale) {
        DisplayMetrics dm = new DisplayMetrics();
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        windowManager.getDefaultDisplay().getMetrics(dm);
        int screenHeight = dm.heightPixels;
        float rate = (scale * (float) screenHeight / 1280);
        Log.e("------>", "==rate==" + rate);
        return rate;
    }

3.webView字体大小

public class ZiTiScale {

    public static void zitiStyle(final Context context, WebView webView) {
        final WebSettings settings = webView.getSettings();
        final SharedPreferences sharedPreferences = context.getSharedPreferences("hgz", Context.MODE_APPEND);
        final SharedPreferences.Editor edit = sharedPreferences.edit();
        String string = sharedPreferences.getString("teda1", null);
        if (string == null) {
            LogUtils.e("------------>", "==string==" + string);
            LogUtils.e("------------>", "默认的字体为小号");
            settings.setSupportZoom(true);
            settings.setBuiltInZoomControls(true);
            settings.setTextSize(WebSettings.TextSize.SMALLER);
        } else {
            LogUtils.e("------------>", "==string==" + string);
            switch (string) {
                case "teda":
                    settings.setSupportZoom(true);
                    settings.setBuiltInZoomControls(true);
                    settings.setTextSize(WebSettings.TextSize.LARGER);
                    break;
                case "dahaozi":
                    settings.setSupportZoom(true);
                    settings.setBuiltInZoomControls(true);
                    settings.setTextSize(WebSettings.TextSize.LARGER);
                    break;
                case "zhonghaozi":
                    settings.setSupportZoom(true);
                    settings.setBuiltInZoomControls(true);
                    settings.setTextSize(WebSettings.TextSize.NORMAL);
                    break;
                case "xiaohaozi":
                    settings.setSupportZoom(true);
                    settings.setBuiltInZoomControls(true);
                    settings.setTextSize(WebSettings.TextSize.SMALLER);
                    break;

            }
        }
    }

    public static void zitiStyle2(final Context context, WebView webView) {
        final WebSettings settings = webView.getSettings();
        final SharedPreferences sharedPreferences = context.getSharedPreferences("hgz", Context.MODE_APPEND);
        final SharedPreferences.Editor edit = sharedPreferences.edit();
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle("修改昵称");
        View view = LayoutInflater.from(context).inflate(R.layout.hgz_ziti_scale, null);
        final TextView teda = (TextView) view.findViewById(R.id.teda);
        final TextView dahaozi = (TextView) view.findViewById(R.id.dahaozi);
        final TextView zhonghaozi = (TextView) view.findViewById(R.id.zhonghaozi);
        final TextView xiaohaozi = (TextView) view.findViewById(R.id.xiaohaozi);
        teda.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                teda.setTextColor(Color.YELLOW);
                settings.setSupportZoom(true);
                settings.setBuiltInZoomControls(true);
                settings.setTextSize(WebSettings.TextSize.LARGEST);
                LogUtils.e("----------->", "点击改为特大号字体");
                edit.putString("teda1", "teda");
                edit.commit();
                LogUtils.e("----------->", "点击改为特大号字体2");
            }
        });
        dahaozi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dahaozi.setTextColor(Color.YELLOW);
                settings.setSupportZoom(true);
                settings.setBuiltInZoomControls(true);
                settings.setTextSize(WebSettings.TextSize.LARGER);
                edit.putString("teda1", "dahaozi");
                edit.commit();
            }
        });
        zhonghaozi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                zhonghaozi.setTextColor(Color.YELLOW);
                settings.setSupportZoom(true);
                settings.setBuiltInZoomControls(true);
                settings.setTextSize(WebSettings.TextSize.NORMAL);
                edit.putString("teda1", "zhonghaozi");
                edit.commit();
            }
        });
        xiaohaozi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                xiaohaozi.setTextColor(Color.YELLOW);
                settings.setSupportZoom(true);
                settings.setBuiltInZoomControls(true);
                settings.setTextSize(WebSettings.TextSize.SMALLER);
                edit.putString("teda1", "xiaohaozi");
                edit.commit();
            }
        });
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        builder.setView(view);//自定义的布局
        //将builder创建
        builder.create();
        //将builder显示
        builder.show();
    }
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值