概述
日常代码中一些用到知识点记录
知识点
四舍五入/保留固定小数位格式化数字显示
1 2 3 4 5 6 7
| public static String format(Locale l, String format, Object... args) { return new Formatter(l).format(format, args).toString(); }
String.format(Locale.CHINA, "%.2f", 5.2252d)
|
** 输出结果:5.23(String)**
使用BigDecimal
类
1 2 3 4 5 6 7 8
| public BigDecimal divide(BigDecimal divisor, int scale, int roundingMode) { return divide(divisor, scale, RoundingMode.valueOf(roundingMode)); }
BigDecimal bigDecimal = new BigDecimal(5.2252d); BigDecimal formatIncome = bigDecimal.divide(new BigDecimal(1), 2, BigDecimal.ROUND_DOWN); String result = ValueOfUtils.stringValue(formatIncome, "0"); }
|
** 输出结果:5.22(String)**
EditText 输入框宽度随着输入字符数变化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| ViewTreeObserver vto2 = edtText.getViewTreeObserver(); vto2.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @SuppressWarnings("deprecation") @Override public void onGlobalLayout() { edtText.getViewTreeObserver().removeGlobalOnLayoutListener(this); mWidth = edtText.getWidth(); } });
edtText.addTextChangedListener(mTextWatcher);
TextWatcher mTextWatcher = new TextWatcher() { @Override public void afterTextChanged(Editable s) { TextPaint mTextPaint =edtText.getPaint(); int textWidth = (int) mTextPaint .measureText(edtText.getText().toString().trim()); RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) edtText .getLayoutParams(); if(s.toString().equals("")){ params.width = mWidth; edtText.setLayoutParams(params); edtText.setHint("hint"); }else { params.width = textWidth; edtText.setLayoutParams(params); } } }; }
|
拦截 EditText 触摸事件,根据条件判断是否显示键盘
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| edtText.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if( event.getAction() == MotionEvent.ACTION_DOWN) { if(TestFlag == false){ TestFlag = true; nameTv.setFocusable(true); nameTv.setFocusableInTouchMode(true); nameTv.requestFocus(); InputMethodManager imm = (InputMethodManager) nameTv.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); imm.toggleSoftInput(0, InputMethodManager.SHOW_FORCED); }else{ TestFlag = false; doSomeThing(); } } return true; } });
|
更好的实现方式,是自定义 View 继承 EditText,然后重写 disPatchTouchEvent 方法;
gradle 查看 Android 引用库的依赖关系命令
1 2 3 4 5 6 7
| Run:
./gradlew -q dependencies <module-name>:dependencies --configuration compile
Example:
./gradlew -q dependencies app:dependencies --configuration compile
|
MEIZU/HUAWEI 等部分手机无法输出 log.d/V 级别的日志问题
Reference
- ** 实测 MEIZU PRO 6 :打开【设置】中的【开发者选项】,页面底部找到【性能优化】,打开【高级日志输出】,勾选【全部允许】即可:**
- ** 华为手机设置方式较为特殊,不是在【开发者选项】中,而是打开拨号界面的拨号盘,输入*##2846579##,系统会自动打开【工程菜单】界面,依次打开【后台设置】 -> 【LOG 设置】,勾选【AP 日志】即可:*
Android Studio 调试详解
简书地址