- 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 <code>dstart … dend</code> of <code>dest</code>
* with the new text from the range <code>start … end</code>
* of <code>source</code>. Return the CharSequence that you would
* like to have placed there instead, including an empty string
* if appropriate, or <code>null</code> 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 <code>dest</code>
* from this method; you may only examine it for context.
* <p>
* Note: If <var>source</var> is an instance of {@link Spanned} or
* {@link Spannable}, the span objects in the <var>source</var> 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()});