【发布时间】:2020-05-04 11:13:17
【问题描述】:
我是 Android 新手,但我卡住了。我在 Activity 上有一个 Textview 应该显示结果,但由于某种原因,只有当您单击不执行任何操作的 Button 或关闭应用程序并从正在运行的应用程序菜单再次重新打开它时,它才会更新 TextView。我想它与更新活动有关。提前谢谢!
package com.example.visacheck;
import android.content.Intent;
import android.os.Bundle;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class FourthActivity extends AppCompatActivity {
String aplicationNumber;
String type;
String year;
@Override
protected void onCreate(Bundle savedInstanceState) {
this.getSupportActionBar().hide();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fourth);
Intent intent = getIntent();
final Button button = findViewById(R.id.resultButton); //Result appearing only after clicking button
aplicationNumber = intent.getStringExtra("aplicationNumber");
type = intent.getStringExtra("type");
year = intent.getStringExtra("year");
class MyJavaScriptInterface {
@JavascriptInterface
@SuppressWarnings("unused")
public void processHTML(String html) {
TextView text = findViewById(R.id.textView);
text.setText(html);
}
}
final WebView myWebview = findViewById(R.id.webview);
myWebview.getSettings().setJavaScriptEnabled(true);
myWebview.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");
myWebview.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
myWebview.loadUrl(("javascript:document.getElementById('edit-ioff-application-number').value = '" + aplicationNumber + "';void(0);"));
myWebview.loadUrl(("javascript:" + "document.getElementById('edit-ioff-application-code').value = '" + type + "';void(0);"));
myWebview.loadUrl(("javascript:document.getElementById('edit-ioff-application-year').value = '" + year + "';void(0);"));
myWebview.loadUrl(("javascript:document.getElementById('edit-submit-button').click();"));
myWebview.loadUrl("javascript:window.HTMLOUT.processHTML(document.getElementsByClassName('alert alert-warning')[0].innerText);");
myWebview.loadUrl("javascript:window.HTMLOUT.processHTML(document.getElementsByClassName('alert alert-success')[1].innerText);");
myWebview.loadUrl("javascript:window.HTMLOUT.processHTML(document.getElementsByClassName('alert alert-danger')[0].innerText);");
}
});
myWebview.loadUrl("https://frs.gov.cz/ioff/application-status");
}
}
【问题讨论】:
-
你能分享一下按钮在onClick中的作用吗?
-
有趣的是,没有onClick方法,这个按钮不做任何事情只存在,但是如果你点击它,TextView正在刷新
-
你的意思是从html设置的textview吗?当您加载页面时,它还没有完成加载 html,因此尚未设置 textview,当您从正在运行的应用程序中打开它时,它会恢复并且已经拥有数据。
-
你能试试这个吗?
myWebView.post(new Runnable() { @Override public void run() { TextView text = findViewById(R.id.textView); text.setText(html); });insde 你的 processHTML 函数 -
@sharone-lev 我认为你是对的,但是我如何设置条件以仅在最终加载 html 时更新 textview?
标签: java android android-activity view textview