??新建android項(xiàng)目,應(yīng)用默認(rèn)配置,其他地方不做修改。新建一個(gè)activity,項(xiàng)目目錄結(jié)構(gòu)為:
公司主營業(yè)務(wù):成都做網(wǎng)站、網(wǎng)站制作、移動(dòng)網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競(jìng)爭能力。創(chuàng)新互聯(lián)公司是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對(duì)我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶帶來驚喜。創(chuàng)新互聯(lián)公司推出和縣免費(fèi)做網(wǎng)站回饋大家。
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);
}
});
}
}
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() {
//設(shè)置在webView點(diǎn)擊打開的新網(wǎng)頁在當(dāng)前界面顯示,而不跳轉(zhuǎn)到新的瀏覽器中
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
webView.getSettings().setJavaScriptEnabled(true); //設(shè)置WebView屬性,運(yùn)行執(zhí)行js腳本
webView.loadUrl("https://"+url+"/"); //調(diào)用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); //調(diào)用Activity提供的setContentView將webView顯示出來
}
//我們需要重寫回退按鈕的時(shí)間,當(dāng)用戶點(diǎn)擊回退按鈕:
//1.webView.canGoBack()判斷網(wǎng)頁是否能后退,可以則goback()
//2.如果不可以連續(xù)點(diǎn)擊兩次退出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();
}
}
}
}
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() {
//設(shè)置在webView點(diǎn)擊打開的新網(wǎng)頁在當(dāng)前界面顯示,而不跳轉(zhuǎn)到新的瀏覽器中
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
webView.getSettings().setJavaScriptEnabled(true); //設(shè)置WebView屬性,運(yùn)行執(zhí)行js腳本
webView.loadUrl("https://www.yiibai.com/android/"); //調(diào)用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); //調(diào)用Activity提供的setContentView將webView顯示出來
}
//我們需要重寫回退按鈕的時(shí)間,當(dāng)用戶點(diǎn)擊回退按鈕:
//1.webView.canGoBack()判斷網(wǎng)頁是否能后退,可以則goback()
//2.如果不可以連續(xù)點(diǎn)擊兩次退出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();
}
}
}
}