版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
【移動應(yīng)用開發(fā)技術(shù)】WebView加載網(wǎng)頁(一)
一、新建一個android項目??新建android項目,應(yīng)用默認配置,其他地方不做修改。新建一個activity,項目目錄結(jié)構(gòu)為:二、修改mainactivitypackagecn.qiu.webview2;
importandroid.content.Intent;
importandroid.support.v7.app.AppCompatActivity;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.widget.Button;
importandroid.widget.EditText;
importandroid.widget.Toast;
publicclassMainActivityextendsAppCompatActivity{
privateEditTexteditText;
privateButtonbutton;
privateButtonbutton2;
@Override
protectedvoidonCreate(BundlesavedInstanceState){
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(newView.OnClickListener(){
@Override
publicvoidonClick(Viewv){
finalStringeditTextMes=editText.getText().toString();
Intentintent=newIntent(MainActivity.this,BaiduActivity.class);
intent.putExtra("url",editTextMes);
Toast.makeText(MainActivity.this,editTextMes,Toast.LENGTH_LONG).show();
startActivity(intent);
}
});
button2.setOnClickListener(newView.OnClickListener(){
@Override
publicvoidonClick(Viewv){
Intentintent=newIntent(MainActivity.this,LearnActivity.class);
startActivity(intent);
}
});
}
}三、修改baiduactivitypackagecn.qiu.webview2;
importandroid.content.Intent;
import.Uri;
importandroid.os.Bundle;
importandroid.support.v7.app.AppCompatActivity;
importandroid.webkit.WebView;
importandroid.webkit.WebViewClient;
importandroid.widget.Toast;
publicclassBaiduActivityextendsAppCompatActivity{
privateWebViewwebView;
privatelongexitTime=0;
@Override
protectedvoidonCreate(BundlesavedInstanceState){
Intentintent=getIntent();
Stringurl=intent.getStringExtra("url");
Toast.makeText(BaiduActivity.this,url,Toast.LENGTH_LONG).show();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_baidu);
webView=newWebView(this);
webView.setWebViewClient(newWebViewClient(){
//設(shè)置在webView點擊打開的新網(wǎng)頁在當前界面顯示,而不跳轉(zhuǎn)到新的瀏覽器中
@Override
publicbooleanshouldOverrideUrlLoading(WebViewview,Stringurl){
view.loadUrl(url);
returntrue;
}
});
webView.getSettings().setJavaScriptEnabled(true);//設(shè)置WebView屬性,運行執(zhí)行js腳本
webView.loadUrl("https://"+url+"/");//調(diào)用loadUrl方法為WebView加入鏈接
webView.setWebViewClient(newWebViewClient(){//
@Override
publicbooleanshouldOverrideUrlLoading(WebViewview,Stringurl){
try{
if(url.startsWith("baiduboxlite://")||url.startsWith("https://")||url.startsWith("baiduboxapp://")){
Intentintent=newIntent(Intent.ACTION_VIEW,Uri.parse(url));
startActivity(intent);
returntrue;
}
}catch(Exceptione){
returnfalse;
}
webView.loadUrl(url);
returntrue;
}
});
setContentView(webView);//調(diào)用Activity提供的setContentView將webView顯示出來
}
//我們需要重寫回退按鈕的時間,當用戶點擊回退按鈕:
//1.webView.canGoBack()判斷網(wǎng)頁是否能后退,可以則goback()
//2.如果不可以連續(xù)點擊兩次退出App,否則彈出提示Toast
@Override
publicvoidonBackPressed(){
if(webView.canGoBack()){
webView.goBack();
}else{
if((System.currentTimeMillis()-exitTime)>2000){
Toast.makeText(getApplicationContext(),"再按一次退出程序",
Toast.LENGTH_SHORT).show();
exitTime=System.currentTimeMillis();
}else{
super.onBackPressed();
}
}
}
}四、修改learnactivitypackagecn.qiu.webview2;
importandroid.content.Intent;
import.Uri;
importandroid.os.Bundle;
importandroid.support.v7.app.AppCompatActivity;
importandroid.webkit.WebView;
importandroid.webkit.WebViewClient;
importandroid.widget.Toast;
publicclassLearnActivityextendsAppCompatActivity{
privateWebViewwebView;
privatelongexitTime=0;
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_learn);
webView=newWebView(this);
webView.setWebViewClient(newWebViewClient(){
//設(shè)置在webView點擊打開的新網(wǎng)頁在當前界面顯示,而不跳轉(zhuǎn)到新的瀏覽器中
@Override
publicbooleanshouldOverrideUrlLoading(WebViewview,Stringurl){
view.loadUrl(url);
returntrue;
}
});
webView.getSettings().setJavaScriptEnabled(true);//設(shè)置WebView屬性,運行執(zhí)行js腳本
webView.loadUrl("/android/");//調(diào)用loadUrl方法為WebView加入鏈接
webView.setWebViewClient(newWebViewClient(){//
@Override
publicbooleanshouldOverrideUrlLoading(WebViewview,Stringurl){
try{
if(url.startsWith("baiduboxlite://")||url.startsWith("https://")||url.startsWith("baiduboxapp://")||url.startsWith("http://")){
Intentintent=newIntent(Intent.ACTION_VIEW,Uri.parse(url));
startActivity(intent);
returntrue;
}
}catch(Exceptione){
returnfalse;
}
webView.loadUrl(url);
returntrue;
}
});
setContentView(webView);//調(diào)用Activity提供的setContentView將webView顯示出來
}
//我們需要重寫回退按鈕的時間,當用戶點擊回退按鈕:
//1.webView.canGoBack()判斷網(wǎng)頁是否能后退,可以則goback()
//2.如果不可以連續(xù)點擊兩次退出App,否則彈出提示Toast
@Override
publicvoidonBackPressed(){
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<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="/apk/res/android"
xmlns:app="/apk/res-auto"
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:layout_width="362dp"
android:layout_height="wrap_content"
android:text="請輸入網(wǎng)址:"
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"/>
<EditText
android:layout_width="364dp"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:hint=""/>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="開始訪問"
android:textSize="40px"
android:textColor="@color/colorPrimaryDark"/>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Android學(xué)習(xí)教程"
android:textColor="@color/colorPrimaryDark"
android:textSize="30px"
android:textAllCaps="false"/>
</LinearLayout>六、baidu_activity.xml<?xmlversion="1.0"encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayoutxmlns:android="/apk/res/android"
xmlns:app="/apk/res-auto"
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".BaiduActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay"/>
</android.support.design.widget.AppBarLayout>
<includelayout="@layout/content_baidu"/>
<android.support.design.widget.FloatingActionButton
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"/>
</android.support.design.widget.CoordinatorLayout>七、learn_activity.xml<?xmlversion="1.0"encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayoutxmlns:android="/apk/res/android"
xmlns:app="/apk/res-auto"
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LearnActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay"/>
</android.support.design.widget.AppBarLayout>
<includelayout="@layout/content_learn"/>
<android.support.design.widget.FloatingActionButton
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"/>
</android.support.design.widg
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2026年軟件測試專家晉級質(zhì)量經(jīng)理題庫詳解
- 外墻通風(fēng)系統(tǒng)設(shè)計與施工方案
- 外墻施工任務(wù)分配方案
- 基于AI的初中化學(xué)實驗現(xiàn)象預(yù)測與實驗安全預(yù)警課題報告教學(xué)研究課題報告
- 水電站發(fā)電效率提升方案
- 隧道施工文檔編制方案
- 排水管網(wǎng)建設(shè)改造項目施工方案
- 病房導(dǎo)視系統(tǒng)優(yōu)化方案
- 施工過程中材料替代方案
- 泰然金融知識競賽
- 別克英朗說明書
- 地下管線測繪課件
- 珍稀植物移栽方案
- 新人教版數(shù)學(xué)三年級下冊預(yù)習(xí)學(xué)案(全冊)
- JJG 810-1993波長色散X射線熒光光譜儀
- GB/T 34336-2017納米孔氣凝膠復(fù)合絕熱制品
- GB/T 20077-2006一次性托盤
- GB/T 1335.3-2009服裝號型兒童
- GB/T 10046-2008銀釬料
- GA 801-2019機動車查驗工作規(guī)程
- 灌注樁后注漿工藝.-演示文稿課件
評論
0/150
提交評論