Android中的小技巧
16lz
2022-01-12
- Android中
ListView
等滚动到某个位置失效
//第一种lv.setSelection(position)//第二种lv.smoothScrollToPosition(position)
第一种直接就是选中位置,第二种有滚动动画,但是有时候,滚动效果看不到,解决方式新建一个runnable,如下:
lv.post(new Runnable() { @Override public void run() { lv.smoothScrollToPosition(position); } });
- 类似的问题,就是使用
SwipeRefreshLayout
的时候,希望打开页面的时候,SwipeRefreshLayout
正在刷新,可是调用SwipeRefreshLayout的setRefreshing(true)
无效,主要原因是页面未渲染完成,解决方式就是延迟执行刷新。
srl.post(new Runnable() { @Override public void run() { srl.setRefreshing(true); } });
EditText
限制输入的小数位数
Android中提供了一个输入过滤器InputFilter
,用于过滤用户输入的内容。
- 定义一个类实现
InputFilter
接口
- 定义一个类实现
public class DoubleInputFilter implements InputFilter { /** * 输入框小数的位数 示例保留一位小数 */ private static final int DECIMAL_DIGITS = 2; /** * This method is called when the buffer is going to replace the * range dstart … dend
of dest
* with the new text from the range start … end
* of source
. Return the CharSequence that you would * like to have placed there instead, including an empty string * if appropriate, or null
to accept the original * replacement. Be careful to not to reject 0-length replacements, * as this is what happens when you delete text. Also beware that * you should not attempt to make any changes to dest
* from this method; you may only examine it for context. * * Note: If source is an instance of {@link Spanned} or * {@link Spannable}, the span objects in the source should be * copied into the filtered result (i.e. the non-null return value). * {@link TextUtils#copySpansFrom} can be used for convenience. * * @param source * @param start * @param end * @param dest * @param dstart * @param dend */
@Override public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { if ("".equals(source.toString())) { return null; } String value = dest.toString(); String[] split = value.split("\\."); if (split.length > 1) { String s = split[1]; int i = s.length() + 1 - DECIMAL_DIGITS; if (i > 0) { return source.subSequence(start, end - i); } } return null; }}
- 使用
et.setFilters(new InputFilter[]{new DoubleInputFilter()});
更多相关文章
- Android用户界面 UI组件--自动提示输入框 AutoCompleteTextView
- Android(安卓)Listview 隐藏滚动条
- Android输入法之输入系统
- Android输入法之——如何禁止横屏时全屏http://blog.csdn.net/na
- Android(安卓)SDK 中常用的几个命令
- Android(安卓)mac 真机调试
- Android(安卓)热修复使用Gradle Plugin1.5改造Nuwa插件
- Android控件TextView中ellipsize属性(设置当文字长度超过textview
- android在使用RecyclerView布局里的androidstudio的模拟器虚拟键