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

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

How to do a second Webview in Android Studio

问题

  1. public class MainActivity extends AppCompatActivity {
  2. private WebView webView;
  3. private WebSettings webSettings;
  4. private WebView webView2;
  5. private WebSettings webSettings2;
  6. @RequiresApi(api = Build.VERSION_CODES.O)
  7. @Override
  8. protected void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.activity_main);
  11. webView = findViewById(R.id.webView);
  12. webSettings = webView.getSettings();
  13. webSettings.setJavaScriptEnabled(true);
  14. webSettings.setLoadWithOverviewMode(true);
  15. webView.setWebViewClient(new WebViewClient());
  16. webView.loadUrl("https://google.de");
  17. webView.clearHistory();
  18. try {
  19. Thread.activeCount();
  20. Thread.sleep(15000); //1000 milliseconds is one second.
  21. webView2 = findViewById(R.id.webView);
  22. webSettings2 = webView2.getSettings();
  23. webSettings2.setJavaScriptEnabled(true);
  24. webSettings2.setLoadWithOverviewMode(true);
  25. webView2.loadUrl("https://google.com");
  26. webView2.clearHistory();
  27. } catch (InterruptedException e) {
  28. e.printStackTrace();
  29. }
  30. }
  31. }
英文:

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。

  1. public class MainActivity extends AppCompatActivity {
  2. private WebView webView;
  3. private WebSettings Websetting;
  4. private WebView webView2;
  5. private WebSettings webSettings2;
  6. @RequiresApi(api = Build.VERSION_CODES.O)
  7. @Override
  8. protected void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.activity_main);
  11. webView = findViewById(R.id.webView);
  12. Websetting = webView.getSettings();
  13. Websetting.setJavaScriptEnabled(true);
  14. Websetting.setLoadWithOverviewMode(true);
  15. webView.setWebViewClient(new WebViewClient());
  16. webView.loadUrl("https://google.de");
  17. webView.clearHistory();
  18. try {
  19. Thread.activeCount();
  20. Thread.sleep(15000); //1000 milliseconds is one second.
  21. webView2 = findViewById(R.id.webView);
  22. Websetting = webView2.getSettings();
  23. Websetting.setJavaScriptEnabled(true);
  24. Websetting.setLoadWithOverviewMode(true);
  25. webView2.loadUrl("https://google.com");
  26. webView2.clearHistory();
  27. }
  28. catch (InterruptedException e)
  29. {
  30. e.printStackTrace();
  31. }
  32. }
  33. }
  34. ´´´
  35. </details>
  36. # 答案1
  37. **得分**: 0
  38. 使用Postdelayed方法尝试:
  39. ```java
  40. public class MainActivity extends AppCompatActivity {
  41. private WebView webView;
  42. private WebSettings Websetting;
  43. private WebView webView2;
  44. private WebSettings webSettings2;
  45. @RequiresApi(api = Build.VERSION_CODES.O)
  46. @Override
  47. protected void onCreate(Bundle savedInstanceState) {
  48. super.onCreate(savedInstanceState);
  49. setContentView(R.layout.activity_main);
  50. webView = findViewById(R.id.webView);
  51. Websetting = webView.getSettings();
  52. Websetting.setJavaScriptEnabled(true);
  53. Websetting.setLoadWithOverviewMode(true);
  54. webView.setWebViewClient(new WebViewClient());
  55. webView.loadUrl("https://google.de");
  56. webView.clearHistory();
  57. new Handler().postDelayed(new Runnable() {
  58. @Override
  59. public void run() {
  60. webView2 = findViewById(R.id.webView);
  61. Websetting = webView2.getSettings();
  62. Websetting.setJavaScriptEnabled(true);
  63. Websetting.setLoadWithOverviewMode(true);
  64. webView2.loadUrl("https://google.com");
  65. webView2.clearHistory();
  66. }
  67. }, 15000);
  68. }
  69. }
  70. }
英文:

Try using Postdelayed method :

  1. public class MainActivity extends AppCompatActivity {
  2. private WebView webView;
  3. private WebSettings Websetting;
  4. private WebView webView2;
  5. private WebSettings webSettings2;
  6. @RequiresApi(api = Build.VERSION_CODES.O)
  7. @Override
  8. protected void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.activity_main);
  11. webView = findViewById(R.id.webView);
  12. Websetting = webView.getSettings();
  13. Websetting.setJavaScriptEnabled(true);
  14. Websetting.setLoadWithOverviewMode(true);
  15. webView.setWebViewClient(new WebViewClient());
  16. webView.loadUrl(&quot;https://google.de&quot;);
  17. webView.clearHistory();
  18. new Handler().postDelayed(new Runnable() {
  19. @Override
  20. public void run() {
  21. webView2 = findViewById(R.id.webView);
  22. Websetting = webView2.getSettings();
  23. Websetting.setJavaScriptEnabled(true);
  24. Websetting.setLoadWithOverviewMode(true);
  25. webView2.loadUrl(&quot;https://google.com&quot;);
  26. webView2.clearHistory();
  27. }
  28. }, 15000);
  29. }
  30. }
  31. }

答案2

得分: 0

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

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

You need to change the following code within the run braces

  1. 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:

确定