Android---webview实现网页的点击跳转

怎样用一个webview就能想浏览器一样随意的浏览网页,并能实时跳转呢?

1.类

public class MyActivity extends Activity implements OnClickListener {

    private WebView findWB;
    protected ProgressBar progressBar;
    private Button find_back;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState)                  requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.weizhang_activity);
        initView();

    }

    @SuppressLint("SetJavaScriptEnabled")
    private void initView() {
        // TODO Auto-generated method stub
         String url = "您的网站网址";

        TextView txtTitle = (TextView) findViewById(R.id.txtTitle);
        txtTitle.setText("标题");

        findWB = (WebView) findViewById(R.id.find_webview);
        find_back = (Button) findViewById(R.id.btnBack);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);
        find_back.setOnClickListener(this);
        findWB.setWebChromeClient(new MyWebChromeClient());//实现页面加载时的进度条功能
        findWB.setWebViewClient(new MyWebViewClient());//这步就是注册监听你在第一个网站上的点击事件,好进行跳转
        // 下面的是进行网站的一些细节的设置------------------------------------------
        findWB.getSettings().setJavaScriptEnabled(true);
        findWB.getSettings().setAppCacheEnabled(true);
        findWB.getSettings().setAllowFileAccess(true);
        findWB.getSettings().setUseWideViewPort(true);
        findWB.getSettings().setLoadWithOverviewMode(true);
        findWB.getSettings().setDomStorageEnabled(true);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
            findWB.getSettings().setDisplayZoomControls(true);
        else
            findWB.getSettings().setBuiltInZoomControls(true);
        findWB.loadUrl(url);

    }

    public class MyWebChromeClient extends WebChromeClient {
        @Override
        public void onProgressChanged(WebView view, int progress) {
            if (progress == 100)
                progress = 0;
            progressBar.setProgress(progress);
        }
    }

    public class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // TODO Auto-generated method stub

            findWB.setWebChromeClient(new MyWebChromeClient());
            findWB.setWebViewClient(new MyWebViewClient());

            // findWB.getSettings().setJavaScriptEnabled(true);
            findWB.getSettings().setAppCacheEnabled(true);
            findWB.getSettings().setAllowFileAccess(true);
            findWB.getSettings().setUseWideViewPort(true);
            findWB.getSettings().setLoadWithOverviewMode(true);
            findWB.getSettings().setDomStorageEnabled(true);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
                findWB.getSettings().setDisplayZoomControls(true);
            else
                findWB.getSettings().setBuiltInZoomControls(true);
            findWB.loadUrl(url);

            return true;
        }

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if ((keyCode == KeyEvent.KEYCODE_BACK) && findWB.canGoBack()) {
        //这里是监控点击返回键时,判断这时的页面是否在第一个网址页面,如果不是就一步步的关掉你打开的新网址
            findWB.goBack();
            return true;
        } else {
            finish();
            return true;
        }

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        switch (v.getId()) {
        case R.id.btnBack:

            if (findWB.canGoBack()) {
            //这里是监控点击返回键时,判断这时的页面是否在第一个网址页面,如果不是就一步步的关掉你打开的新网址
                findWB.goBack();
            } else {
                finish();
                return;
            }

            break;

        default:
            break;
        }

    }

}

2.布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <include layout="@layout/csy_titlebar"/>


    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:indeterminate="false"
        android:progressDrawable="@drawable/progress_drawable" />

    <WebView
        android:id="@+id/find_webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

3.csy_titlebar布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/titlebar"
    android:layout_width="fill_parent"
    android:layout_height="45dp"
    android:layout_alignParentTop="true"
    android:background="#0091ff" >

    <Button
        android:id="@+id/btnBack"
        android:layout_width="50.0dp"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:background="@android:color/transparent"
        android:drawableLeft="@drawable/csy_arr_back"
        android:drawablePadding="5.0dip"
        android:gravity="center_vertical"
        android:text="返回"
        android:textColor="@android:color/white"
        android:textSize="14.0sp" />

    <TextView
        android:id="@+id/txtTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:gravity="center"
        android:text="@string/app_name"
        android:textColor="@android:color/white"
        android:textSize="18.0sp" />

</RelativeLayout>

4.progress_drawable进度条的布局

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/background">
        <color android:color="#0091ff" />
    </item>
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <solid android:color="#ffffff" />
            </shape>
        </clip>
    </item>
</layer-list>
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值