TextView在创建新活动后未更新

问题描述 投票:0回答:2

我是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");
    }

}
java android android-activity view textview
2个回答
0
投票

@ Khodor的解决方案解决了我的问题,现在它就像一种魅力。谢谢@Khodor!

final WebView myWebview = findViewById(R.id.webview);

        class MyJavaScriptInterface {
            @JavascriptInterface
            @SuppressWarnings("unused")
            public void processHTML(final String html) {
                /*TextView text = findViewById(R.id.textView);
                text.setText(html);*/
                myWebview.post(new Runnable() { @Override public void run() { TextView text = findViewById(R.id.textView); text.setText(html); }});
            }
        }

0
投票

我不会在这方面做得很好,因为我自己几乎无法触及这个主题。也许有更多知识的人可以进一步详细介绍,但是这里的总体思路是,此代码是由JS线程而不是UI线程(这是唯一允许处理UI更新的线程)调用的,我老实说,这行代码不会崩溃时,我感到很惊讶。 post()方法将其添加到视图的消息队列中,这意味着将在呈现视图后更新文本,并且没有其他要执行的任务。我知道我在解释这一点上做得不好,但是有关更多信息,请参考以下内容:

What exactly does the post method do?

或者,您可以使用runOnUIThread(),例如:

How do we use runOnUiThread in Android?

我确信外面有很多人比我解释得更好。但是,这里最重要的事情是您一定不要从UI线程以外的任何内容更新UI

[请注意,我任意选择了myWebView,并且如果也发布到该片段的主视图中,这也应该起作用。

class MyJavaScriptInterface {
        @JavascriptInterface
        @SuppressWarnings("unused")
        public void processHTML(final String html) {
            /*TextView text = findViewById(R.id.textView);
            text.setText(html);*/
            myWebview.post(new Runnable() {
                @Override public void run() {
                    TextView text = findViewById(R.id.textView); text.setText(html);
                }
            });
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.