gpt4 book ai didi

Android Webview - 如何在默认浏览器中打开外部链接?

转载 作者:行者123 更新时间:2023-11-29 15:40:05 27 4
gpt4 key购买 nike

我正在尝试制作一个 webview 应用程序,但我不太擅长。

我只是想这样做:我有一个网站,我想在默认浏览器中打开外部链接,例如目标_blank。

我的 Activity :

public class MainActivity extends AppCompatActivity {

DrawerLayout drawerLayout;
ActionBarDrawerToggle drawerToggle;
NavigationView navigation;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OneSignal.startInit(this).init();
initInstances();
WebView myWebView = (WebView) findViewById(R.id.webView1);
myWebView.loadUrl("http://mywebsite.domain.com/myprofile/");
myWebView.setWebViewClient(new WebViewClient());
}

private void initInstances() {
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
drawerToggle = new ActionBarDrawerToggle(MainActivity.this, drawerLayout, R.string.hello_world, R.string.hello_world);
drawerLayout.setDrawerListener(drawerToggle);

navigation = (NavigationView) findViewById(R.id.navigation_view);
navigation.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {

String menu1 = "https://mywebsite.domain.com/myprofile/";
String menu2 = "https://mywebsite.domain.com/exit";
String menu3 = "https://mywebsite.domain.com/discover/";
String menu4 = "https://mywebsite.domain.com/settings/";
String menu5 = "https://mywebsite.domain.com/language";
String menu6 = "https://mywebsite.domain.com/vipmembership";

@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
int id = menuItem.getItemId();
WebView myWebView = (WebView) findViewById(R.id.webView1);
myWebView.setWebViewClient(new WebViewClient());
switch (id) {
case R.id.navigation_item_1:
myWebView.loadUrl(this.menu1);
break;
case R.id.navigation_item_2:
myWebView.loadUrl(this.menu2);
break;
case R.id.navigation_item_3:
myWebView.loadUrl(this.menu3);
break;
case R.id.navigation_item_4:
myWebView.loadUrl(this.menu4);
break;
case R.id.navigation_item_5:
myWebView.loadUrl(this.menu5);
break;
case R.id.navigation_item_6:
myWebView.loadUrl(this.menu6);
break;
}
return false;
}
});


}

@Override
public void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (drawerToggle.onOptionsItemSelected(item))
return true;

// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}}

我厌倦了很多关于它的代码,但它不起作用。非常感谢。

最佳答案

您必须向默认网络浏览器发送一个 Intent,以便在浏览器中打开链接,所以可能是这样的:

创建一个扩展 WebViewClient 的类,如下所示:

private class MyCustomWebViewClient extends WebViewClient {

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.contains("https://mywebsite.domain.com")) {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(i);
} else {
view.loadUrl(url);
}
return true;
}
}

在 MainActivity 的 onCreate 方法中,在调用 loadUrl 方法之前设置 MyCustomWebViewClient,如下所示:

public class MainActivity extends AppCompatActivity {

DrawerLayout drawerLayout;
ActionBarDrawerToggle drawerToggle;
NavigationView navigation;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OneSignal.startInit(this).init();
initInstances();
WebView myWebView = (WebView) findViewById(R.id.webView1);
myWebView.setWebViewClient(new MyCustomWebViewClient());
myWebView.loadUrl("http://mywebsite.domain.com/myprofile/");
}

// the rest of your code

关于Android Webview - 如何在默认浏览器中打开外部链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42100833/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com