WebView加载网页(一)

WebView加载网页

一、新建一个android项目

  新建android项目,应用默认配置,其他地方不做修改。新建一个activity,项目目录结构为:

二、修改mainactivity

package cn.qiu.webview2;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private EditText editText;
    private Button button;
    private Button button2;

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

        button=(Button)findViewById(R.id.button);
        editText=(EditText)findViewById(R.id.editText);
        button2=(Button)findViewById(R.id.button2);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String editTextMes=editText.getText().toString();
                Intent intent=new Intent(MainActivity.this,BaiduActivity.class);
                intent.putExtra("url",editTextMes);
                Toast.makeText(MainActivity.this,editTextMes,Toast.LENGTH_LONG).show();
                startActivity(intent);
            }
        });

        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.this,LearnActivity.class);
                startActivity(intent);
            }
        });
    }
}

三、修改baiduactivity

package cn.qiu.webview2;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class BaiduActivity extends AppCompatActivity {

        private WebView webView;
        private long exitTime = 0;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                Intent intent=getIntent();
                String url=intent.getStringExtra("url");
                Toast.makeText(BaiduActivity.this,url,Toast.LENGTH_LONG).show();

                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_baidu);
                webView = new WebView(this);
                webView.setWebViewClient(new WebViewClient() {
                        //设置在webView点击打开的新网页在当前界面显示,而不跳转到新的浏览器中
                        @Override
                        public boolean shouldOverrideUrlLoading(WebView view, String url) {
                                view.loadUrl(url);
                                return true;
                        }
                });
                webView.getSettings().setJavaScriptEnabled(true);  //设置WebView属性,运行执行js脚本
                webView.loadUrl("https://"+url+"/");          //调用loadUrl方法为WebView加入链接
                webView.setWebViewClient(new WebViewClient(){    //
                        @Override
                        public boolean shouldOverrideUrlLoading(WebView view, String url) {

                                try{
                                        if(url.startsWith("baiduboxlite://")||url.startsWith("https://")||url.startsWith("baiduboxapp://")){
                                                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                                                startActivity(intent);
                                                return true;
                                        }
                                }catch (Exception e){
                                        return false;
                                }
                                webView.loadUrl(url);
                                return true;
                        }
                });
                setContentView(webView);                           //调用Activity提供的setContentView将webView显示出来
        }

        //我们需要重写回退按钮的时间,当用户点击回退按钮:
        //1.webView.canGoBack()判断网页是否能后退,可以则goback()
        //2.如果不可以连续点击两次退出App,否则弹出提示Toast
        @Override
        public void onBackPressed() {
                if (webView.canGoBack()) {
                        webView.goBack();
                } else {
                        if ((System.currentTimeMillis() - exitTime) > 2000) {
                                Toast.makeText(getApplicationContext(), "再按一次退出程序",
                                                Toast.LENGTH_SHORT).show();
                                exitTime = System.currentTimeMillis();
                        } else {
                                super.onBackPressed();
                        }

                }
        }

}

四、修改learnactivity

package cn.qiu.webview2;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class LearnActivity extends AppCompatActivity {
        private WebView webView;
        private long exitTime = 0;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_learn);
                webView = new WebView(this);
                webView.setWebViewClient(new WebViewClient() {
                        //设置在webView点击打开的新网页在当前界面显示,而不跳转到新的浏览器中
                        @Override
                        public boolean shouldOverrideUrlLoading(WebView view, String url) {
                                view.loadUrl(url);
                                return true;
                        }
                });
                webView.getSettings().setJavaScriptEnabled(true);  //设置WebView属性,运行执行js脚本
                webView.loadUrl("https://www.yiibai.com/android/");          //调用loadUrl方法为WebView加入链接
                webView.setWebViewClient(new WebViewClient(){    //
                        @Override
                        public boolean shouldOverrideUrlLoading(WebView view, String url) {

                                try{
                                        if(url.startsWith("baiduboxlite://")||url.startsWith("https://")||url.startsWith("baiduboxapp://")||url.startsWith("http://")){
                                                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                                                startActivity(intent);
                                                return true;
                                        }
                                }catch (Exception e){
                                        return false;
                                }
                                webView.loadUrl(url);
                                return true;
                        }
                });
                setContentView(webView);                           //调用Activity提供的setContentView将webView显示出来
        }

        //我们需要重写回退按钮的时间,当用户点击回退按钮:
        //1.webView.canGoBack()判断网页是否能后退,可以则goback()
        //2.如果不可以连续点击两次退出App,否则弹出提示Toast
        @Override
        public void onBackPressed() {
                if (webView.canGoBack()) {
                        webView.goBack();
                } else {
                        if ((System.currentTimeMillis() - exitTime) > 2000) {
                                Toast.makeText(getApplicationContext(), "再按一次退出程序",
                                                Toast.LENGTH_SHORT).show();
                                exitTime = System.currentTimeMillis();
                        } else {
                                super.onBackPressed();
                        }

                }
        }

}

五、修改activy_main.xml




        

        

        

六、baidu_activity.xml




    

        

    

    

    

七、learn_activity.xml




        

                

        

        

        

八、androidmanifest.xml




        

        
                
                        
                                

                                
                        
                
                
                
        

九、运行效果

1、首页

2、百度页面

3、Android学习页面

转载于:https://blog.51cto.com/doublelinux/2344027

你可能感兴趣的:(WebView加载网页(一))