如何在编辑文本中设置WebView中的主页?

时间:2014-08-15 13:08:37

标签: android webview

我有一个webview,我想在其中添加通过编辑文本字段更改主页的功能。像所有的浏览器一样,但来自其他活动(我希望在另一个活动中拥有此设置和所有其他设置)。有可能这样做吗?感谢

        package com.example.app;

        import android.app.Activity;
        import android.app.AlertDialog;
        import android.content.DialogInterface;
        import android.content.Intent;
        import android.net.Uri;
        import android.os.Bundle;
        import android.view.Menu;
        import android.view.View;
        import android.view.View.OnClickListener;
        import android.webkit.CookieManager;
        import android.webkit.DownloadListener;
        import android.webkit.WebChromeClient;
        import android.webkit.WebSettings;
        import android.webkit.WebSettings.PluginState;
        import android.webkit.WebView;
        import android.webkit.WebViewClient;
        import android.widget.Button;
        import android.widget.CompoundButton;
        import android.widget.CompoundButton.OnCheckedChangeListener;
        import android.widget.EditText;
        import android.widget.ProgressBar;
        import android.widget.Toast;
        import android.widget.ToggleButton;

        public class MainActivity extends Activity {
            private WebView webView;
            private EditText urlEditText;
            private ProgressBar progress;

            private ToggleButton getToggleButton(int id) {
                return (ToggleButton) findViewById(id);
            }

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

                getToggleButton(R.id.leave_toggle).setOnCheckedChangeListener(
                        new OnCheckedChangeListener() {
                            @Override
                            public void onCheckedChanged(CompoundButton toggleButton,
                                    boolean isChecked) {
                                if (isChecked) {
                                    webView.getSettings().setLoadsImagesAutomatically(
                                            false);// Enable Image
                                    // Loading
                                } else {
                                    webView.getSettings().setLoadsImagesAutomatically(
                                            true);// Enable Image
                                    // Loading
                                }
                            }
                        });

                urlEditText = (EditText) findViewById(R.id.urlField);
                webView = (WebView) findViewById(R.id.webView);
                webView.setWebChromeClient(new MyWebViewClient());
                webView.setDownloadListener(new DownloadListener() {// Download Manager
                    public void onDownloadStart(String url, String userAgent,
                            String contentDisposition, String mimetype,
                            long contentLength) {
                        Intent i = new Intent(Intent.ACTION_VIEW);
                        i.setData(Uri.parse(url));
                        startActivity(i);
                    }
                });
                CookieManager.getInstance().setAcceptCookie(true);// Enable Cookies
                webView.getSettings().setJavaScriptEnabled(true);// Enable Java Script
                webView.setWebViewClient(new HelloWebViewClient());
                webView.loadUrl("http://www.google.com/"); // Set
                                                            // Home
                                                            // page
                webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);// Remove
                                                                            // ScrollBars
                webView.getSettings().setDefaultFontSize(12);// Set Font Size

                webView.getSettings().setPluginState(PluginState.ON);// Enable Flash
                webView.setBackgroundColor(0x00000000);// Transparent Screen When
                                                        // Loading
                webView.getSettings().setBuiltInZoomControls(true);// Set Zoom
                // Controls
                webView.requestFocus(View.FOCUS_DOWN);// Enable WebView Interaction

                webView.getSettings().setAppCacheMaxSize(1024 * 1024 * 8);// Set Cache
                                                                            // (8mb)
                String appCachePath = getApplicationContext().getCacheDir()
                        .getAbsolutePath();// Set Cache (8mb)
                webView.getSettings().setAppCachePath(appCachePath);// Set Cache (8mb)
                webView.getSettings().setAllowFileAccess(true);// Set Cache (8mb)
                webView.getSettings().setAppCacheEnabled(true);// Set Cache (8mb)
                webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);// Set
                                                                                // Cache
                                                                                // (8mb)

                webView.setWebViewClient(new WebViewClient() {// Open URL on Error
                    @Override
                    public void onReceivedError(WebView view, int errorCode,
                            String description, String failingUrl) {// Open URL on Error
                        Intent intent = new Intent(MainActivity.this, Error.class);
                        startActivity(intent);
                    }
                });

                // ////8888888888888888**URL BAR AND PROGRESS
                // BAR**888888888888888888888888888888888

                progress = (ProgressBar) findViewById(R.id.progressBar);
                progress.setMax(100);

                Button openUrl = (Button) findViewById(R.id.goButton);
                openUrl.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View view) {
                        String url = urlEditText.getText().toString();

                        if (url.endsWith(".ac") || url.endsWith(".ac.uk")

                            if (!url.startsWith("http://")
                                    && !url.startsWith("https://")) {
                                url = "http://" + url;
                            }
                        }
                        if (validateUrl(url)) {
                            webView.getSettings().setJavaScriptEnabled(true);
                            webView.loadUrl(url);

                            MainActivity.this.progress.setProgress(10);
                        }
                    }

                    private boolean validateUrl(String url) {
                        return true;
                    }
                });

            }

            private class MyWebViewClient extends WebChromeClient {
                @Override
                public void onProgressChanged(WebView view, int newProgress) {
                    MainActivity.this.setValue(newProgress);
                    super.onProgressChanged(view, newProgress);
                }
            }

            @Override
            public boolean onCreateOptionsMenu(Menu menu) {
                getMenuInflater().inflate(R.menu.main, menu);
                return true;
            }

            public void setValue(int progress) {
                this.progress.setProgress(progress);
            }

            // ////8888888888888888**URL BAR AND PROGRESS
            // BAR**888888888888888888888888888888888

            class HelloWebViewClient extends WebViewClient {
                @Override
                public boolean shouldOverrideUrlLoading(WebView webview, String url) {

                    webview.loadUrl(url);
                    return true;
                }
            }

            public void forward(View view) { // Forward Button
                Toast toast = Toast.makeText(this, "Forward", Toast.LENGTH_LONG);
                toast.show();
                webView.goForward();
            }

            public void back(View view) { // Back Button
                Toast toast = Toast.makeText(this, "Back", Toast.LENGTH_LONG);
                toast.show();
                webView.goBack();
            }

            public void stop(View view) { // Stop Button
                Toast toast = Toast.makeText(this, "Stop", Toast.LENGTH_LONG);
                toast.show();
                webView.stopLoading();
            }

            public void reload(View view) { // Reload Button
                Toast toast = Toast.makeText(this, "Reloading..", Toast.LENGTH_LONG);
                toast.show();
                webView.reload();
            }

            // Tools navigation
            public void database(View view) {
                Intent intent = new Intent(this, Edt_Txt.class);
                Toast toast = Toast.makeText(this, "Memory", Toast.LENGTH_LONG);
                toast.show();
                startActivity(intent);
            }

            public void mini(View view) {
                Intent startMain = new Intent(Intent.ACTION_MAIN);
                startMain.addCategory(Intent.CATEGORY_HOME);
                startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                Toast toast = Toast.makeText(this, "Minimized", Toast.LENGTH_LONG);
                toast.show();
                startActivity(startMain);
            }



            public void json(View view) {
                Intent intent = new Intent(this, JSON.class);
                Toast toast = Toast.makeText(this, "Loading JSON (BETA)",
                        Toast.LENGTH_LONG);
                toast.show();
                startActivity(intent);
            }

            @Override
            public void onBackPressed() {
                new AlertDialog.Builder(this)
                        .setIcon(android.R.drawable.ic_dialog_alert)
                        .setTitle("Exit")
                        .setMessage("Are you sure you want to exit?")
                        .setPositiveButton("Yes",
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog,
                                            int which) {
                                        finish();
                                        System.exit(0);
                                    }
                                }).setNegativeButton("No", null).show();
            }
        }

1 个答案:

答案 0 :(得分:0)

当您点击将默认网址设置为主页时,这是以下代码:

  SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
  SharedPreferences.Editor editor = preferences.edit();
  editor.putString("URL","http"//sample.com");
  editor.apply();

当你将url设置为浏览器时,请执行以下操作:

  SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
  String url= preferences.getString("URL","");
  if(!name.equalsIgnoreCase(""))
  {
    //set url in broswer u will always get as last setted home page url
  }