在Android中,要使用內(nèi)置的瀏覽器,需要通過(guò)WebView組件來(lái)實(shí)現(xiàn),核心是開(kāi)源WebKit引擎。
創(chuàng)新互聯(lián)于2013年創(chuàng)立,先為大新等服務(wù)建站,大新等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為大新企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問(wèn)題。WebView是專門用來(lái)瀏覽網(wǎng)頁(yè)的,既可以在XML文件中使用
WebView最簡(jiǎn)單的應(yīng)用就是在布局文件中定義一個(gè)WebView組件,在程序代碼中實(shí)例化組件,并調(diào)用其loadUrl方法,傳入需要訪問(wèn)的地址即可。
步驟:
1.布局文件中定義WebView
2.實(shí)例化WebView
WebView webview=(WebView)findviewbyId(R.id.xxx);
webview.loadUrl("www.baidu.com");
3.別忘了在清單文件中加入權(quán)限
下面是一個(gè)有趣又簡(jiǎn)單的瀏覽器實(shí)例:
WebGo.java代碼:
(使用了GridView,詳細(xì)GridView代碼不再給出)
public class WebGo extends Activity {
public static String flag="id";
GridView gridview;
int[] p_w_picpaths=new int[]{R.drawable.neihanshequ,R.drawable.wangyi,R.drawable.kuailemahua,R.drawable.baidu,R.drawable.taobao,R.drawable.kuaidi,
R.drawable.pengfu,R.drawable.shenmasousuo,R.drawable.manhua,R.drawable.xiaohua,R.drawable.wenti,R.drawable.jiuyou,R.drawable.jiuyou,R.drawable.yxlm,R.drawable.logo17173};
String[] titles=new String[]{"內(nèi)涵段子","網(wǎng)易新聞","快樂(lè)麻花","百度一下","逛淘寶","查快遞","捧腹網(wǎng)","神馬搜索","最火漫畫","經(jīng)典搞笑","腦筋急轉(zhuǎn)彎","今日新服","免費(fèi)單機(jī)","游戲資訊","17173主頁(yè)"};
String string1="http://m.neihanshequ.com";
String string2="http://3g.163.com/touch/";
String string3="http://m.mahua.com";
String string4="http://m.baidu.com/?from=2001a";
String string5="http://m.taobao.com";
String string6="http://m.kuaidi100.com";
String string7="http://m.pengfu.com";
String string8="http://m.sa.sm.cn";
String string9="http://m。dmzj。com";
String string10="http://info.3g.qq.com";
String string11="http://m.xuexila。com/jzw/";
String string12="http://a.9game.cn/xinfu/";
String string13="http://a.9game.cn/danji2/";
String string14="http://m.baidu.com";
String string15="http://m.17173.com";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webgo);
gridview=(GridView)findViewById(R.id.gv1);
PictureAdapter2 adapter=new PictureAdapter2(titles,p_w_picpaths,this);
gridview.setAdapter(adapter);
gridview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView> arg0, View arg1, int arg2,
long arg3) {
Intent intent=null;
switch(arg2){
case 0:intent=new Intent(WebGo.this,WebGoOn.class);
intent.putExtra(flag, string1);
startActivity(intent);
finish();
break;
case 1:intent=new Intent(WebGo.this,WebGoOn.class);
intent.putExtra(flag, string2);
startActivity(intent);
finish();
break;
case 2:intent=new Intent(WebGo.this,WebGoOn.class);
intent.putExtra(flag, string3);
startActivity(intent);
finish();
break;
case 3:intent=new Intent(WebGo.this,WebGoOn.class);
intent.putExtra(flag, string4);
startActivity(intent);
finish();
break;
case 4:intent=new Intent(WebGo.this,WebGoOn.class);
intent.putExtra(flag, string5);
startActivity(intent);
finish();
break;
case 5:intent=new Intent(WebGo.this,WebGoOn.class);
intent.putExtra(flag, string6);
startActivity(intent);
finish();
break;
case 6:intent=new Intent(WebGo.this,WebGoOn.class);
intent.putExtra(flag, string7);
startActivity(intent);
finish();
break;
case 7:intent=new Intent(WebGo.this,WebGoOn.class);
intent.putExtra(flag, string8);
startActivity(intent);
finish();
break;
case 8:intent=new Intent(WebGo.this,WebGoOn.class);
intent.putExtra(flag, string9);
startActivity(intent);
finish();
break;
case 9:intent=new Intent(WebGo.this,WebGoOn.class);
intent.putExtra(flag, string10);
startActivity(intent);
finish();
break;
case 10:intent=new Intent(WebGo.this,WebGoOn.class);
intent.putExtra(flag, string11);
startActivity(intent);
finish();
break;
case 11:intent=new Intent(WebGo.this,WebGoOn.class);
intent.putExtra(flag, string12);
startActivity(intent);
finish();
break;
case 12:intent=new Intent(WebGo.this,WebGoOn.class);
intent.putExtra(flag, string13);
startActivity(intent);
finish();
break;
case 13:intent=new Intent(WebGo.this,WebGoOn.class);
intent.putExtra(flag, string14);
startActivity(intent);
finish();
break;
case 14:intent=new Intent(WebGo.this,WebGoOn.class);
intent.putExtra(flag, string15);
startActivity(intent);
finish();
break;
}
}
});
}
public boolean onKeyDown(int KeyCode,KeyEvent Event){
if(KeyCode==KeyEvent.KEYCODE_BACK){
Intent intent=new Intent(WebGo.this,MainActivity.class);
startActivity(intent);
finish();
}
return true;
}
}
WebGoOn.java代碼:
public class WebGoOn extends Activity {
private WebView webview;
private ImageButton p_w_picpathbutton1,p_w_picpathbutton2,p_w_picpathbutton3;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webgoon);
p_w_picpathbutton1=(ImageButton)findViewById(R.id.imgbtn1);
p_w_picpathbutton2=(ImageButton)findViewById(R.id.imgbtn2);
p_w_picpathbutton3=(ImageButton)findViewById(R.id.imgbtn3);
webview=(WebView)findViewById(R.id.webView1);
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebChromeClient(new WebChromeClient());
webview.setWebViewClient(new WebViewClient());
Intent intent=getIntent();
Bundle bundle=intent.getExtras();
String string=bundle.getString(WebGo.flag);
webview.loadUrl(string);
p_w_picpathbutton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
webview.goBack();
}
});
p_w_picpathbutton3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent=new Intent(WebGoOn.this,WebGo.class);
startActivity(intent);
finish();
}
});
p_w_picpathbutton2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
webview.goForward();
}
});
}
public boolean onKeyDown(int KeyCode,KeyEvent Event){
if(KeyCode==KeyEvent.KEYCODE_BACK){
webview.goBack();
}
return true;
}
}
webgoon.xml代碼:
android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="#cccccc" > android:layout_width="match_parent" android:layout_height="match_parent" > android:id="@+id/webView1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/imgbtn1" /> android:id="@+id/imgbtn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:src="@drawable/back" /> android:id="@+id/imgbtn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:src="@drawable/go" /> android:id="@+id/imgbtn3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:src="@drawable/end" />
主要代碼就是這些,效果圖如下:
很簡(jiǎn)的。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。