android webview加载动画,WebView加载网页(一)

WebView加载网页

一、新建一个android项目

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

848a2fa7fd323916ceb50b295b4882db.png

二、修改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

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity"

android:orientation="vertical">

android:layout_width="362dp"

android:layout_height="wrap_content"

android:text="请输入网址: "

android:textSize="50px"

android:textColor="@color/colorAccent"

app:layout_constraintBottom_toBottomOf="parent"

app:layout_constraintLeft_toLeftOf="parent"

app:layout_constraintRight_toRightOf="parent"

app:layout_constraintTop_toTopOf="parent" />

android:layout_width="364dp"

android:layout_height="wrap_content"

android:id="@+id/editText"

android:hint=" " />

android:id="@+id/button"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="开始访问"

android:textSize="40px"

android:textColor="@color/colorPrimaryDark"/>

android:id="@+id/button2"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="Android学习教程"

android:textColor="@color/colorPrimaryDark"

android:textSize="30px"

android:textAllCaps="false"/>

六、baidu_activity.xml

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".BaiduActivity">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:theme="@style/AppTheme.AppBarOverlay">

android:id="@+id/toolbar"

android:layout_width="match_parent"

android:layout_height="?attr/actionBarSize"

android:background="?attr/colorPrimary"

app:popupTheme="@style/AppTheme.PopupOverlay" />

android:id="@+id/fab"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="bottom|end"

android:layout_margin="@dimen/fab_margin"

app:srcCompat="@android:drawable/ic_dialog_email" />

七、learn_activity.xml

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".LearnActivity">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:theme="@style/AppTheme.AppBarOverlay">

android:id="@+id/toolbar"

android:layout_width="match_parent"

android:layout_height="?attr/actionBarSize"

android:background="?attr/colorPrimary"

app:popupTheme="@style/AppTheme.PopupOverlay" />

android:id="@+id/fab"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="bottom|end"

android:layout_margin="@dimen/fab_margin"

app:srcCompat="@android:drawable/ic_dialog_email" />

八、androidmanifest.xml

package="cn.qiu.webview2">

android:allowBackup="true"

android:icon="@mipmap/ic_launcher"

android:label="@string/app_name"

android:roundIcon="@mipmap/ic_launcher_round"

android:supportsRtl="true"

android:theme="@style/AppTheme">

android:name=".BaiduActivity"

android:label="@string/title_activity_baidu"

android:theme="@style/AppTheme.NoActionBar" />

android:name=".LearnActivity"

android:label="@string/title_activity_learn"

android:theme="@style/AppTheme.NoActionBar">

九、运行效果

1、首页

ef0e7fd8472ebe2d690aa692d1ae6c19.png

2、百度页面

866e7d46f8fc6a158d9b7af4b33044b7.png

3、Android学习页面

7e66d815d13338eaca0a99f9378d730c.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值