我们有一个 HTML 格式的按钮。当用户点击它时,我的特定 EditText
的值应该改变,但它没有。
代码:
class ActivityTest extends Activity {
TextView textView1;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
WebView webView = (WebView) findViewById(R.id.webView);
webView.loadUrl("file:///android_res/raw/htmlimages.html");
webView.addJavascriptInterface(new MyTest(), "Scripts");
webView.getSettings().setJavaScriptEnabled(true);
textView1 = (TextView) findViewById(R.id.textView1);
}
public class MyTest {
void setText(String string)
{
textView1.setText(string);
}
}
}
最佳答案
假设您的 html 部分是正确的,您需要做的一件事是在主线程上调用 ui。
所以你的代码:
void setText(String string)
{
textView1.setText(string);
}
应该是
@JavascriptInterface
public void setText() {
runOnUiThread(new Runnable() {
@Override
public void run() {
textView.setText("Testing text");
}
});
}
关于javascript - 如何使用 Javascript 更改 EditText 内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23509579/