由于 Android 不支持显示日期,我在布局中添加了一个 TextView。
我在 DatePicker 上放置了一个 onDateChangedListener。当日期更改时,我确定星期几,然后通过处理程序设置它以确保它在 UI 线程上完成:
// this is being called when the date changes on the DatePicker
public void onDateChanged(DatePicker view, final int year,
final int monthOfYear, final int dayOfMonth) {
String dayOfTheWeek = null;
// some code here to set the 'dayOfTheWeek' string
h.sendMessage(h.obtainMessage(0, dayOfTheWeek));
}
final Handler h = new Handler() {
@Override
public void handleMessage(Message msg) {
dayOfWeekTextView.setText((String)msg.obj);
}
};
我的问题:当我在小部件 (Android 4.0 U I) 中滚动浏览日期时,它并不流畅!我检查了问题所在,这不是我确定字符串应该是什么的方式,它实际上是处理程序中的 setText()
。评论那个部分,一切都很好。还在那里设置硬编码文本 (dayOfWeekTextView.setText("SOMETHING');
) 会产生同样的问题。它会使 DatePicker
抖动。
setText()
真的有那么大的 Action 吗?奇妙 list 会显示星期几,而且运行很流畅,但我就是不知道他们是怎么做到的。
我只在我的 HTC One X
上测试过它。
有人有想法吗?
Edit:
I found out why it is smooth in Wunderlist! I am using the same as them, a DialogFragment. You can set a title on the dialog and that is what Wunderlist uses instead of a TextView. So I'm going to use that (and looks better too ;) )
So when doing 'getDialog().setTitle(dayOfWeek)' there is no problem. Still the question remains: how come the setText is that slow on the UI thread...?
最佳答案
性能不佳可能是重新计算布局的结果。如果 textview 宽度为 wrap-content,则每次文本更改时,布局都会更新。 尝试固定 TextView 的宽度。
关于android - 在处理程序性能差的 TextView 中设置文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14556934/