android - 如何在下次运行时选择webview的url首页?

时间:2017-12-01 03:32:12

标签: android webview

我编辑了我的问题。我需要在下次运行应用程序时选择webview的url startpage。我想使用sharedpreferences,但我没有这样做。活动中的另一个代码存在问题,我的代码和代码无法收集。我不知道要添加什么更多解释,我希望你们能理解我的精神。我只需要共享偏好代码而无需更改主代码。 下面的代码不应该被更改,因为如果更改它,另一个具有共享偏好的活动将无效:

String current_page_url = "https://website.com/az";

startwebview();

webView.loadUrl(current_page_url);

public void startwebview(){
webView = (WebView) findViewById(R.id.webView);
webView.loadUrl("https://website.com/az");

这是我的主要代码:

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {


private ProgressBar progressBar;

WebView webView;
String current_page_url = "https://website.com/az";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

startwebview();

webView = (WebView) findViewById(R.id.webView);
    initWebView();
    webView.loadUrl(current_page_url);

}

private void initWebView() {
    webView.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            progressBar.setVisibility(View.VISIBLE);
            current_page_url = url;
            invalidateOptionsMenu();
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            webView.loadUrl(url);
            return true;
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                webView.loadUrl(request.getUrl().toString());
            }
            return true;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            progressBar.setVisibility(View.GONE);
            invalidateOptionsMenu();
        }

        @Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
            super.onReceivedError(view, request, error);
            progressBar.setVisibility(View.GONE);
            invalidateOptionsMenu();
        }
    });

   WebSettings webSettings = webView.getSettings();
    webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
    webView.getSettings().setBuiltInZoomControls(true);
    webView.getSettings().setSupportZoom(true);
    webView.getSettings().setDisplayZoomControls(false);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.setWebChromeClient(new WebChromeClient());

}

 @Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId())
    {
        case R.id.ru:
            webView.loadUrl("https://website.com/ru");

    ***i want add sharedpreferences here and save this page for startpage on next run of application***


            Toast.makeText(this, "Page 1 is set!", Toast.LENGTH_SHORT).show();
            return true;
    }
    switch (item.getItemId())
    {
        case R.id.en:
            webView.loadUrl("https://website.com/en);

 ***i want add sharedpreferences here and save this page for startpage on next run of application***


            Toast.makeText(this, "Page 2 is set!", Toast.LENGTH_SHORT).show();
            return true;
    }

 }


  public void startwebview(){
    webView = (WebView) findViewById(R.id.webView);
    webView.loadUrl("https://website.com/az");
}

3 个答案:

答案 0 :(得分:0)

在这里,您可以在选择网页后保存您的偏好

switch (item.getItemId())
    {
        case R.id.ru:
        webView.loadUrl("https://website.com/ru");

        SharedPreferences.Editor editor = getSharedPreferences("YOUR_PREF", MODE_PRIVATE).edit();
         editor.putString("url", "https://website.com/ru");
         editor.commit();


        Toast.makeText(this, "Page 1 is set!", Toast.LENGTH_SHORT).show();
        return true;
}
switch (item.getItemId())
{
    case R.id.en:
        webView.loadUrl("https://website.com/en");

         SharedPreferences.Editor editor = getSharedPreferences("YOUR_PREF", MODE_PRIVATE).edit();
         editor.putString("url", "https://website.com/en");
         editor.commit();

        Toast.makeText(this, "Page 2 is set!", Toast.LENGTH_SHORT).show();
        return true;
}

然后阅读onCreate方法的首选项。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

startwebview();
initWebView();

SharedPreferences prefs = getSharedPreferences("YOUR_PREF", MODE_PRIVATE); 
String restoredURL = prefs.getString("url", null);
if (restoredURL != null) {
    webView = (WebView) findViewById(R.id.webView);
    webView.loadUrl(restoredURL);
}


}

答案 1 :(得分:0)

保存上次访问过的页面

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()){
        case R.id.ru:{
            webView.loadUrl("https://website.com/ru");
            Toast.makeText(this, "Page 1 is set!", Toast.LENGTH_SHORT).show();
            SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPref.edit();
            editor.putString(WEB_LINKS, "https://website.com/ru");
            editor.commit();
            return true;}
        case R.id.en:{
            webView.loadUrl("https://website.com/en");
                    Toast.makeText(this, "Page 2 is set!", Toast.LENGTH_SHORT).show();
            SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPref.edit();
            editor.putString(WEB_LINKS, "https://website.com/en");
            editor.commit();
            return true;
        }
        default: return false;
    }

}

阅读上次访问过的页面

SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
    String lastSavedPage = sharedPref.getString(WEB_LINKS,current_page_url);

如果没有保存,您的默认页面将为https://website.com/az

答案 2 :(得分:0)

只需在课程开始时添加此行::

      SharedPreferences prefs;
      SharedPreferences.Editor editor;

现在在onCreate()方法中初始化它:

      prefs = getSharedPreferences(PREFERENCE, MODE_PRIVATE);
      Editor = = getSharedPreferences(PREFERENCE, MODE_PRIVATE).edit();

现在更改“onOptionSelectedItem”:

我们将两个开关结合起来。因为没必要

 switch (item.getItemId()) 
 { 
      case R.id.ru: 
      { 
          webView.loadUrl("https://website.com/ru"); 
           editor.putString("WEB_LINKS", "https://website.com/ru"); 
           editor.apply();

         Toast.makeText(this, "Page 1 is set!", Toast.LENGTH_SHORT).show(); 

      } 
       case R.id.en: 
       { 
           webView.loadUrl("https://website.com/en"); 
           editor.putString("WEB_LINKS", "https://website.com/en"); 
           editor.apply();

           Toast.makeText(this, "Page 2 is set!", Toast.LENGTH_SHORT).show(); return true; 
        } 
 }
 return true;

现在在startWebView ::

进行更改
 public void startwebview()
 { 
      webView = (WebView) findViewById(R.id.webView); 
      webView.loadUrl(prefs.getString("name", "www.google.com")); 
 }

在这里,你完成了所有事情..设置共享偏好并让它在开始时加载。