我正在尝试制作一个webview应用程序,但我并不擅长。
我想这样做:我有一个网站,我想在默认浏览器中打开外部链接,如目标_blank。
我的活动:
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);
}}
我厌倦了这么多关于它的代码,但它没有用。非常感谢。
答案 0 :(得分:3)
你必须发送一个Intent到默认的Web浏览器,以便在浏览器中打开链接,所以可能是这样的:
以这种方式创建一个扩展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