美文网首页
常用的小技术点收集

常用的小技术点收集

作者: 落叶随风花落入泥 | 来源:发表于2019-04-18 17:06 被阅读0次

    常用的小技术点:

    1、给RadioButton设置图片的时候,我们想设置图片的大小(代码设置),

    Drawable drawable = getResources().getDrawable(R.mipmap.menu_location);
    drawable.setBounds(0, 0, 60, 60); //这是我项目中的要求,这里根据自己情况满足要求填写不是固定的
    contentfragmentRbtnHome.setCompoundDrawables(drawable, null, null, null);
    

    RadioButton代码设置如下:

         <RadioButton
            android:id="@+id/contentfragment_rbtn_home"
            style="@style/RadioButtonStyle"
            android:layout_marginTop="5dp"
            android:drawableLeft="@drawable/selected_home_btn_bg"
            android:drawablePadding="10dp"
            android:text="@string/map_page"
            android:textColor="@color/color_black" />
    

    2、fragment切换代码

     /**
     * 切换fragment
     *
     * @param position
     */
    public void shouFragment(int position) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        hideFragments(transaction);
        switch (position) {
    
            case 0:
    
                if (mapFragment == null) {
                    mapFragment = (MapFragment) fragments.get(0);
                    transaction.add(R.id.fl_frameLayout, mapFragment);
                } else {
                    if (!mapFragment.isAdded()) {
                        transaction.add(R.id.fl_frameLayout, mapFragment);
                    }
                    transaction.show(mapFragment);
                }
                break;
            case 1:
                if (workingConditionFragment == null) {
                    workingConditionFragment = (ReportFormsFragment) fragments.get(1);
                    transaction.add(R.id.fl_frameLayout, workingConditionFragment);
                } else {
                    if (!workingConditionFragment.isAdded()) {
                        transaction.add(R.id.fl_frameLayout, workingConditionFragment);
                    }
                    transaction.show(workingConditionFragment);
                }
                break;
            case 2:
                if (parameterMonitoringFragment == null) {
                    parameterMonitoringFragment = (ParameterMonitoringFragment) fragments.get(2);
                    transaction.add(R.id.fl_frameLayout, parameterMonitoringFragment);
                } else {
                    if (!parameterMonitoringFragment.isAdded()) {
                        transaction.add(R.id.fl_frameLayout, parameterMonitoringFragment);
                    }
                    transaction.show(parameterMonitoringFragment);
                }
                break;
    
            case 3:
                if (alarmListFragment == null) {
                    alarmListFragment = (AlarmListFragment) fragments.get(3);
                    transaction.add(R.id.fl_frameLayout, alarmListFragment);
                } else {
                    if (!alarmListFragment.isAdded()) {
                        transaction.add(R.id.fl_frameLayout, alarmListFragment);
                    }
                    transaction.show(alarmListFragment);
                }
                break;
            case 4:
                if (maintenanceServiceFragment == null) {
                    maintenanceServiceFragment = (MaintenanceServiceFragment) fragments.get(4);
                    transaction.add(R.id.fl_frameLayout, maintenanceServiceFragment);
                } else {
                    if (!maintenanceServiceFragment.isAdded()) {
                        transaction.add(R.id.fl_frameLayout, maintenanceServiceFragment);
                    }
                    transaction.show(maintenanceServiceFragment);
                }
                break;
    
            case 5:
                if (settingFragment == null) {
                    settingFragment = (SettingFragment) fragments.get(5);
                    transaction.add(R.id.fl_frameLayout, settingFragment);
                } else {
                    if (!settingFragment.isAdded()) {
                        transaction.add(R.id.fl_frameLayout, settingFragment);
                    }
                    transaction.show(settingFragment);
                }
    
                break;
    
            case 6:
    
                if (meFragment == null) {
                    meFragment = (MeFragment) fragments.get(6);
                    transaction.add(R.id.fl_frameLayout, meFragment);
                } else {
                    if (!meFragment.isAdded()) {
                        transaction.add(R.id.fl_frameLayout, meFragment);
                    }
                    transaction.show(meFragment);
                }
                break;
    
            case 7:
                if (homeFragment == null) {
                    homeFragment = (HomeFragment) fragments.get(7);
                    transaction.add(R.id.fl_frameLayout, homeFragment);
                } else {
    
                    if (!homeFragment.isAdded()) {
                        transaction.add(R.id.fl_frameLayout, homeFragment);
                    }
                    transaction.show(homeFragment);
                }
                break;
        }
        transaction.commit();
    }
    
    /**
     * 隐藏所有的fragment
     */
    private void hideFragments(FragmentTransaction transaction) {
        if (homeFragment != null) {
    
            transaction.hide(homeFragment);
        }
        if (workingConditionFragment != null) {
    
            transaction.hide(workingConditionFragment);
        }
        if (mapFragment != null) {
    
            transaction.hide(mapFragment);
        }
    
        if (meFragment != null) {
    
            transaction.hide(meFragment);
        }
    
        if (settingFragment != null) {
    
            transaction.hide(settingFragment);
        }
    
        if (alarmListFragment != null) {
            transaction.hide(alarmListFragment);
        }
    
        if (parameterMonitoringFragment != null) {
            transaction.hide(parameterMonitoringFragment);
    
        }
        if (maintenanceServiceFragment != null) {
    
            transaction.hide(maintenanceServiceFragment);
        }
    
    }
    

    3、获取Color的几种方式

    #11223344 格式,11位表示透明度,为0时完全透明,为255时完全不透明,后三个分量依次表示R,G,B颜色 
    参数
    
    获取Color的几种方式:
        int c1 = this.getResources().getColor(R.color.colorTest);
        int c2 = Color.parseColor("#FF0000");
        int c3 = 0xFF0000;
        int c4 = 0xFFFF0000;
        int c5= Color.argb(255,255,0,0);
    

    4、隐藏软件盘的方法:

    #通常我们的需求是,当我们停止操作输入,点击空白处,需要隐藏软键盘,在base中借助事件分发方式解决 
    代码如下:
    
    /**
     * 在Activity中点击其他部位隐藏软键盘
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        InputMethodManager manager = (InputMethodManager) 
        getSystemService(Context.INPUT_METHOD_SERVICE);
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            if (getCurrentFocus() != null && getCurrentFocus().getWindowToken() != null) {
                manager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 
                InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
        return super.onTouchEvent(event);
    }
    
     /**
     * 在Fragment中点击其他部位隐藏软键盘
     */
      mView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                InputMethodManager manager = (InputMethodManager) 
                getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    if (getActivity().getCurrentFocus() != null && getActivity().getCurrentFocus().getWindowToken() != 
                    null) {
                        manager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 
                        InputMethodManager.HIDE_NOT_ALWAYS);
                    }
                }
                return false;
            }
          });
    
    /**
     * 这个是让输入法状态发生逆转,如果当前未显示则显示出来。如果显示出来,则隐藏。
     */
    public void hidekeybord() {
        InputMethodManager systemService = (InputMethodManager) 
        getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (systemService != null) {
            systemService.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
        }
       }
    
     /**
     * 其它不变,隐藏软件盘的方法参数,还可以如下:
     */
     systemService .hideSoftInputFromWindow(passwdEdit.getWindowToken(), 0);
    
     以上四种情况根据各自的情况进行选择,解决问题
    

    5、选择器:

    android:state_pressed
    
         一个布尔值,如果这个项目是在对象被按下时使用,那么就要设置为true。(如,按钮被触摸或点击 
    时。)false应该用于默认的非按下状态。
    
    android:state_focused
    
         一个布尔值,如果这个项目是在对象获取焦点时使用,那么就要设置为true。如,一个选项标签被打开 
    时。如果这个项目要用于对象没有被被选择的时候,那么就要设置为false。
    
    android:state_checkable
    
         一个布尔值,如果这个项目要用于对象的可选择状态,那么就要设置为true。如果这个项目要用于不可选 
    状态,那么就要设置为false。(它只用于一个对象在可选和不可选之间的转换)。
    
    android:state_checked
    
         一个布尔值,如果这个项目要用于对象被勾选的时候,那么就要设置为true。否者设为false。
    
    android:state_enabled
    
         一个布尔值,如果这个项目要用于对象可用状态(接受触摸或点击事件的能力),那么就要设置为true, 
     否者设置为false。
    
     事例代码:
     <?xml version="1.0" encoding="utf-8"?>
     <selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:drawable="@drawable/switch_bg_disabled_emui" android:state_enabled="false"/>
     <item android:drawable="@drawable/switch_bg_on_emui" android:state_pressed="true"/>
     <item android:drawable="@drawable/switch_bg_on_emui" android:state_focused="true"/>
     <item android:drawable="@drawable/switch_bg_on_emui" android:state_checked="true"/>
     <item android:drawable="@drawable/switch_bg_off_emui"/>
     </selector>
    

    6、获取状态栏的高度,代码如下:

        /**
     * 获取状态栏高度
     *
     * @return
     */
    protected int getStatusBarHeight() {
        Class<?> c = null;
        Object obj = null;
        Field field = null;
        int x = 0, sbar = 20;//,我项目是20dp,有的资料说是38dp这个根据自己详情进行判断
        try {
            c = Class.forName("com.android.internal.R$dimen");
            obj = c.newInstance();
            field = c.getField("status_bar_height");
            x = Integer.parseInt(field.get(obj).toString());
            sbar = getResources().getDimensionPixelSize(x);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return sbar;
        }
    }
    

    7、简单权限判断工具类,解决项目中多出使用危险权限,重复编写权限判断代码。(用第三方框架除外)

    public class PermisionUtils {
    // Storage Permissions
    private static final int REQUEST_EXTERNAL_STORAGE = 1;
    private static String[] PERMISSIONS_STORAGE = {Manifest.permission.READ_EXTERNAL_STORAGE, 
    Manifest.permission.WRITE_EXTERNAL_STORAGE};
    
    
    public static void verifyStoragePermissions(Activity activity) {
        int permission = ActivityCompat.checkSelfPermission(activity, 
    Manifest.permission.WRITE_EXTERNAL_STORAGE);
        if (permission != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE, 
    REQUEST_EXTERNAL_STORAGE);
        }
    
    }
    }
    

    8、沉浸式状态栏工具类

    public class StatusBarColorUtils {
    private StatusBarColorUtils() {
    
    }
    
    public static void setStatusBarColor(Activity activity, int color) {
        /**
         * 浸入式
         */
        if (hasLollipop()) {
            Window window = activity.getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(color);
        }
    }
    
    public static boolean hasLollipop() {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
    }
    }
    

    相关文章

      网友评论

          本文标题:常用的小技术点收集

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