英文:
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
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("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);
}
}
}
答案2
得分: 0
你需要在运行大括号内更改以下代码:
webView2 = findViewById(R.id.webView2);
英文:
You need to change the following code within the run braces
webView2 = findViewById(R.id.webView2);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论