项目中踩过的坑,记录一下
Android中定义test方法,H5调用后更新页面TextView的显示
错误的方式:
@JavascriptInterface
public void test(String str) {
tv.setText(str);
}
如直接setText,则无法正确显示str的值
正确方式:
@JavascriptInterface
public void test(String str) {
runOnUiThread(() -> {
try {
tv.setText(str);
} catch (Exception e) {
e.printStackTrace();
}
});
}
究其原因的话,H5调用Android代码时,所调用的方法运行在子线程中,然子线程无法更新UI,故setText无效