Android WebView基础开发——Android菜鸟成长日记

距离上一次更新已经是20天了,是我太懒了,该打。
今天是简单开始一点关于安卓的知识了,时间太紧迫,没有时间去详细的了解关于算法的知识,当然我不会落下算法的学习,只是先把安卓基础知识往前推进了一点。

老规矩还是线上官方链接
WebView API Android Developer

代码

MainActivity.java

package wlhwh.example.com.study20161222webview;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {

    private WebView webview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webview=(WebView)findViewById(R.id.webview);
        //设置webview的属性,能够执行javascript脚本
        webview.getSettings().setJavaScriptEnabled(true);
        //设置需要打开网页的网址
        webview.loadUrl("https://wangxiaochou.github.io");
        //设置web视图
        webview.setWebViewClient(new HelloWebViewClient());
    }

    //设置回退
    //设置Activity类的keyDown(int keyCode,KeyEvent event)
    @Override
    public boolean onKeyDown(int keyCode,KeyEvent event ){
        if((keyCode == KeyEvent.KEYCODE_BACK)&&(webview.canGoBack())){
            //goBack表示返回web的上一个页面
            webview.goBack();
            return true;
        }
        return true;
    }

    private class HelloWebViewClient extends WebViewClient{
        @Override
        public boolean shouldOverrideUrlLoading(WebView view,String url){
            view.loadUrl(url);
            return true;
        }
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="wlhwh.example.com.study20161222webview.MainActivity">

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

</RelativeLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="wlhwh.example.com.study20161222webview">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-permission android:name="android.permission.INTERNET"/>
</manifest>

截图

这里写图片描述

今天就暂时到这里

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值