您好,登录后才能下订单哦!
在Android中,要使用内置的浏览器,需要通过WebView组件来实现,核心是开源WebKit引擎。
WebView是专门用来浏览网页的,既可以在XML文件中使用<WebView>标记添加,又可以在Java文件中通过new关键字创建,推荐使用XML方法。
WebView最简单的应用就是在布局文件中定义一个WebView组件,在程序代码中实例化组件,并调用其loadUrl方法,传入需要访问的地址即可。
步骤:
1.布局文件中定义WebView
2.实例化WebView
WebView webview=(WebView)findviewbyId(R.id.xxx);
webview.loadUrl("www.baidu.com");
3.别忘了在清单文件中加入权限
<uses-permission android:name="android.permission.INTERNET"/>
下面是一个有趣又简单的浏览器实例:
WebGo.java代码:
(使用了GridView,详细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[]{"内涵段子","网易新闻","快乐麻花","百度一下","逛淘宝","查快递","捧腹网","神马搜索","最火漫画","经典搞笑","脑筋急转弯","今日新服","免费单机","游戏资讯","17173主页"};
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代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#cccccc" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/imgbtn1" />
<ImageButton
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" />
<ImageButton
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" />
<ImageButton
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" />
</RelativeLayout>
</LinearLayout>
主要代码就是这些,效果图如下:
很简的。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。