如何在EditText中显示当前网址 - android studio

时间:2017-12-04 07:58:27

标签: javascript java android android-edittext

我正在尝试创建一个Web浏览器但是我遇到的问题是我无法在EditText中显示当前的URL。从Webview中显示currrent url。我怎样才能做到这一点。请告诉我应该在哪里放置代码以及我需要放置什么代码。请帮帮我。

这是我的代码:

public class MainActivity extends AppCompatActivity {

    WebView browser;
    EditText urlbox;
    Button go,forward,backward,refresh,clear;
    ProgressBar progressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        browser= (WebView)findViewById(R.id.browser);
        urlbox = (EditText)findViewById(R.id.urlbox);
        go = (Button)findViewById(R.id.go);
        forward = (Button)findViewById(R.id.f);
        backward = (Button)findViewById(R.id.b);
        refresh = (Button)findViewById(R.id.r);
        clear = (Button)findViewById(R.id.c);
        progressBar = (ProgressBar) findViewById(R.id.progressbar);

        browser.setWebViewClient(new ourviewclient());


        browser.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                progressBar.setProgress(newProgress);
                if(newProgress == 100){
                    progressBar.setVisibility(view.GONE);
                }else {
                    progressBar.setVisibility(view.VISIBLE);
                }
            }
        });

        WebSettings javascript = browser.getSettings();
        browser.getSettings().setJavaScriptEnabled(true);



        go.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

               String editextvalue = urlbox.getText().toString();

               if(!editextvalue.startsWith("http://"))
                   editextvalue = "http://" + editextvalue;

               String url = editextvalue;

               browser.loadUrl(url);

               //for keyboard hide
                InputMethodManager keyboard = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                keyboard.hideSoftInputFromWindow(urlbox.getWindowToken(),0);
            }
        });

        forward.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (browser.canGoForward())
                    browser.goForward();
            }
        });

        backward.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (browser.canGoBack())
                    browser.goBack();
            }
        });

        refresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                browser.reload();
            }
        });

        clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                browser.clearHistory();
            }
        });

    }
}

1 个答案:

答案 0 :(得分:1)

试试这个

browser.setWebViewClient(new WebViewClient() {    
      @Override
      public void onPageStarted(WebView view, String url, Bitmap favicon) {
           super.onPageStarted(view, url, favicon);

           // set here your URL in Your EditText
            urlbox.setText(url);    

       }    

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

       @Override
       public void onPageFinished(WebView view, String url) {

       }
  });