如何在Android Studio中创建第二个Webview。

huangapple go评论120阅读模式
英文:

How to do a second Webview in Android Studio

问题

public class MainActivity extends AppCompatActivity {
    private WebView webView;
    private WebSettings webSettings;
    private WebView webView2;
    private WebSettings webSettings2;

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webView = findViewById(R.id.webView);
        webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setLoadWithOverviewMode(true);
        webView.setWebViewClient(new WebViewClient());
        webView.loadUrl("https://google.de");
        webView.clearHistory();

        try {
            Thread.activeCount();
            Thread.sleep(15000); //1000 milliseconds is one second.
            webView2 = findViewById(R.id.webView);
            webSettings2 = webView2.getSettings();
            webSettings2.setJavaScriptEnabled(true);
            webSettings2.setLoadWithOverviewMode(true);
            webView2.loadUrl("https://google.com");
            webView2.clearHistory();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
英文:

inside my Webview App I want to call a second website after a quick pause.

Unfortunately it looks like the pause between the first and the second link dont work...

I dont know why can u give me a tip pls 如何在Android Studio中创建第二个Webview。


public class MainActivity extends AppCompatActivity {
 private WebView webView;
 private WebSettings Websetting;
 private WebView webView2;
 private WebSettings webSettings2;

 @RequiresApi(api = Build.VERSION_CODES.O)
 @Override
 protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     webView = findViewById(R.id.webView);
     Websetting = webView.getSettings();
     Websetting.setJavaScriptEnabled(true);
     Websetting.setLoadWithOverviewMode(true);
     webView.setWebViewClient(new WebViewClient());
     webView.loadUrl("https://google.de");
     webView.clearHistory();

     try {
         Thread.activeCount();
         Thread.sleep(15000); //1000 milliseconds is one second.
         webView2 = findViewById(R.id.webView);
         Websetting = webView2.getSettings();
         Websetting.setJavaScriptEnabled(true);
         Websetting.setLoadWithOverviewMode(true);
         webView2.loadUrl("https://google.com");
         webView2.clearHistory();
     }
     catch (InterruptedException e)
     {
         e.printStackTrace();

     }

 }
}

´´´










</details>


# 答案1
**得分**: 0

使用Postdelayed方法尝试:

```java
public class MainActivity extends AppCompatActivity {
private WebView webView;
private WebSettings Websetting;
private WebView webView2;
private WebSettings webSettings2;

@RequiresApi(api = Build.VERSION_CODES.O)
@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  webView = findViewById(R.id.webView);
  Websetting = webView.getSettings();
  Websetting.setJavaScriptEnabled(true);
  Websetting.setLoadWithOverviewMode(true);
  webView.setWebViewClient(new WebViewClient());
  webView.loadUrl("https://google.de");
  webView.clearHistory();


      new Handler().postDelayed(new Runnable() {


         @Override
         public void run() {
             webView2 = findViewById(R.id.webView);
             Websetting = webView2.getSettings();
             Websetting.setJavaScriptEnabled(true);
             Websetting.setLoadWithOverviewMode(true);
             webView2.loadUrl("https://google.com");
             webView2.clearHistory();
         }
     }, 15000);
  }


}
}
英文:

Try using Postdelayed method :

public class MainActivity extends AppCompatActivity {
 private WebView webView;
 private WebSettings Websetting;
 private WebView webView2;
 private WebSettings webSettings2;

 @RequiresApi(api = Build.VERSION_CODES.O)
 @Override
 protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     webView = findViewById(R.id.webView);
     Websetting = webView.getSettings();
     Websetting.setJavaScriptEnabled(true);
     Websetting.setLoadWithOverviewMode(true);
     webView.setWebViewClient(new WebViewClient());
     webView.loadUrl(&quot;https://google.de&quot;);
     webView.clearHistory();

    
         new Handler().postDelayed(new Runnable() {


            @Override
            public void run() {
                webView2 = findViewById(R.id.webView);
                Websetting = webView2.getSettings();
                Websetting.setJavaScriptEnabled(true);
                Websetting.setLoadWithOverviewMode(true);
                webView2.loadUrl(&quot;https://google.com&quot;);
                webView2.clearHistory();
            }
        }, 15000);
     }
    

 }
}

答案2

得分: 0

你需要在运行大括号内更改以下代码:

webView2 = findViewById(R.id.webView2);
英文:

You need to change the following code within the run braces

webView2 = findViewById(R.id.webView2);

huangapple
  • 本文由 发表于 2020年10月15日 19:11:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/64370294.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定