2016-07-27 220 views
-1
package com.slimolobeapp.slimolobe; 

import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.view.KeyEvent; 
import android.view.View; 
import android.support.design.widget.NavigationView; 
import android.support.v4.view.GravityCompat; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBarDrawerToggle; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.webkit.WebSettings; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 

import java.net.URL; 

public class MainActivity extends AppCompatActivity 
     implements NavigationView.OnNavigationItemSelectedListener { 

    //initilizing webview 
    private WebView mywebView; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
         .setAction("Action", null).show(); 
      } 
     }); 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
       this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
     drawer.setDrawerListener(toggle); 
     toggle.syncState(); 

     NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
     navigationView.setNavigationItemSelectedListener(this); 

     mywebView = (WebView) findViewById(R.id.myWebView); 
     WebSettings webSettings = mywebView.getSettings(); 
     webSettings.setJavaScriptEnabled(true); 
     mywebView.loadUrl("http://www.slimolobeapp.com"); 
     //force links open in webview 
     mywebView.setWebViewClient(new MyWebClient()); 
    } 

    @Override 
    public void onBackPressed() { 
     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     if (drawer.isDrawerOpen(GravityCompat.START)) { 
      drawer.closeDrawer(GravityCompat.START); 
     } else { 
      super.onBackPressed(); 
     } 
    } 

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

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // 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); 
    } 

    @SuppressWarnings("StatementWithEmptyBody") 
    @Override 
    public boolean onNavigationItemSelected(MenuItem item) { 
     // Handle navigation view item clicks here. 
     int id = item.getItemId(); 

     if (id == R.id.nav_shop) { 
      mywebView.loadUrl("http://www.slimolobeapp.com/shop"); 
     } else if (id == R.id.nav_category) { 
      mywebView.loadUrl("http://www.slimolobeapp.com/category"); 
     } else if (id == R.id.nav_price) { 
      mywebView.loadUrl("http://www.slimolobeapp.com/price"); 
     } else if (id == R.id.nav_contact) { 
      mywebView.loadUrl("http://www.slimolobeapp.com/contact"); 
     } else if (id == R.id.nav_facebook) { 
      mywebView.loadUrl("http://www.facebook.com/slimolobe"); 
     } else if (id == R.id.nav_instagram) { 
      mywebView.loadUrl("http://www.instagram.com/slimolobe"); 
     } 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 
     return true; 
    } 

    private class MyWebClient extends WebViewClient { 
     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) { 
      if (Uri.parse(url).getHost().equals("www.slimolobeapp.com")) { 
//open url content in webview 
       return false; 
      } else { 
//here open external links in external browser or app 
       Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); 
       startActivity(intent); 
       return true; 
      } 
     } 
    } 


    //goto previous page when pressing back button 

    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
     if (event.getAction() == KeyEvent.ACTION_DOWN) { 
      switch (keyCode) { 
       case KeyEvent.KEYCODE_BACK: 
        if (mywebView.canGoBack()) { 
         mywebView.goBack(); 
        } else { 
         finish(); 
        } 
        return true; 
      } 

      return super.onKeyDown(keyCode, event); 
     } 


     return false; 

    } 
} 

這是我的主要活動代碼,我不知道我在哪裏做錯了。每當我打開應用程序,網絡視圖啓動瀏覽器,應用程序不加載網頁,它只加載瀏覽器,我不想這樣。Android應用程序webview在瀏覽器中打開而不是應用程序

+0

您遇到任何錯誤 – vinoth12594

+0

沒有,我沒有得到一個錯誤,加載網頁在瀏覽器中,而不是應用程序 –

+0

添加Internet權限在manifest資源配置文件<使用許可權的android: name =「android.permission.INTERNET」/> – vinoth12594

回答

0

我相信你在調用loadUrl()之前需要設置你的WebView客戶端。

+0

好吧,我會試試 –

+0

它是否工作,我很想知道問題是什麼? – Ben

+0

它不起作用。其實代碼是正確的,但我的網站不配置爲允許www,所以我必須重新配置它以允許www。感謝您的幫助 –

-1

試試這個代碼

import android.app.Activity; 
import android.os.Bundle; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
import android.widget.Toast; 

public class Main extends Activity { 

    private WebView mWebview ; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 

     mWebview = new WebView(this); 

     mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript 

     final Activity activity = this; 

     mWebview.setWebViewClient(new WebViewClient() { 
      public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { 
       Toast.makeText(activity, description, Toast.LENGTH_SHORT).show(); 
      } 
     }); 

     mWebview .loadUrl("http://www.google.com"); 
     setContentView(mWebview); 

    } 

} 
+0

你會在這個答案中添加一些評論,這將如何幫助?我們通常不鼓勵只有代碼的答案,因爲他們可能不清楚現在或將來的讀者。 – halfer

相關問題